freegate 0.6.6 → 0.6.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/lib/cache.js +14 -2
- package/lib/normalize.js +74 -0
- package/lib/providers.js +1 -1
- package/lib/routing.js +60 -0
- package/package.json +1 -1
- package/providers.json +40 -30
- package/server.js +31 -20
package/lib/cache.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
const crypto = require('crypto');
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
|
+
const { normalizeMessages } = require('./normalize');
|
|
5
6
|
|
|
6
7
|
const MAX_SIZE = 500;
|
|
7
8
|
const DEFAULT_TTL = 3600000; // 1 hour in ms
|
|
@@ -9,17 +10,28 @@ const MAX_ENTRY_BYTES = 256 * 1024; // don't persist responses larger than 256KB
|
|
|
9
10
|
const CACHE_PATH = path.join(__dirname, '..', 'cache.json');
|
|
10
11
|
|
|
11
12
|
class LRUCache {
|
|
12
|
-
constructor(maxSize = MAX_SIZE, ttl = DEFAULT_TTL, skipLoad = false) {
|
|
13
|
+
constructor(maxSize = MAX_SIZE, ttl = DEFAULT_TTL, skipLoad = false, useNormalize = false) {
|
|
13
14
|
this.maxSize = maxSize;
|
|
14
15
|
this.ttl = ttl;
|
|
15
16
|
this.cache = new Map();
|
|
16
17
|
this.hits = 0;
|
|
17
18
|
this.misses = 0;
|
|
19
|
+
this.useNormalize = useNormalize;
|
|
18
20
|
if (!skipLoad) this.load();
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
_key(model, messages, temperature) {
|
|
22
|
-
|
|
24
|
+
let raw;
|
|
25
|
+
if (this.useNormalize) {
|
|
26
|
+
const norm = normalizeMessages(messages);
|
|
27
|
+
// Пустая нормализация (только картинки/инструменты без текста) НЕ должна
|
|
28
|
+
// схлопывать разные запросы в один ключ — fallback на точное совпадение.
|
|
29
|
+
raw = norm
|
|
30
|
+
? `${model}|${norm}|${temperature || 0}`
|
|
31
|
+
: `${model}|${JSON.stringify(messages)}|${temperature || 0}`;
|
|
32
|
+
} else {
|
|
33
|
+
raw = `${model}|${JSON.stringify(messages)}|${temperature || 0}`;
|
|
34
|
+
}
|
|
23
35
|
return crypto.createHash('sha256').update(raw).digest('hex').slice(0, 16);
|
|
24
36
|
}
|
|
25
37
|
|
package/lib/normalize.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Нормализация сообщений для семантического кэша.
|
|
2
|
+
// Сравниваются только пользовательские текстовые сообщения, без system.
|
|
3
|
+
|
|
4
|
+
const MAX_LEN = 2000;
|
|
5
|
+
|
|
6
|
+
const CODE_SYMBOLS = new Set(['{', '}', '[', ']', '=>', ';', '=', '+', '-', '*', '/', '<', '>']);
|
|
7
|
+
const CODE_KEYWORDS = new Set([
|
|
8
|
+
'function', 'const', 'let', 'var', 'return', 'import', 'export',
|
|
9
|
+
'class', 'def', '=>', 'await', 'async', 'try', 'catch', 'throw',
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
function lastUserText(messages) {
|
|
13
|
+
if (!Array.isArray(messages)) return '';
|
|
14
|
+
const users = messages.filter(m => m && m.role === 'user');
|
|
15
|
+
const last = users[users.length - 1];
|
|
16
|
+
if (!last) return '';
|
|
17
|
+
if (typeof last.content === 'string') return last.content;
|
|
18
|
+
if (Array.isArray(last.content)) {
|
|
19
|
+
return last.content.filter(c => c && c.type === 'text').map(c => c.text || '').join(' ');
|
|
20
|
+
}
|
|
21
|
+
return '';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function looksLikeCode(messages) {
|
|
25
|
+
const text = lastUserText(messages);
|
|
26
|
+
if (!text) return false;
|
|
27
|
+
if (text.includes('```')) return true;
|
|
28
|
+
|
|
29
|
+
const hasKeyword = [...CODE_KEYWORDS].some(kw => new RegExp(`\\b${kw}\\b`).test(text));
|
|
30
|
+
|
|
31
|
+
// Считаем операторы/символы; стрелку `=>` учитываем как отдельный символ,
|
|
32
|
+
// чтобы `=` и `>` внутри неё не задваивались.
|
|
33
|
+
let symbolCount = 0;
|
|
34
|
+
const rest = text.replace(/=>/g, '');
|
|
35
|
+
if (text.includes('=>')) symbolCount += 1;
|
|
36
|
+
for (const ch of CODE_SYMBOLS) {
|
|
37
|
+
if (ch === '=>') continue;
|
|
38
|
+
symbolCount += rest.split(ch).length - 1;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (hasKeyword && symbolCount >= 1) return true;
|
|
42
|
+
if (symbolCount >= 3) return true;
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function normalizeMessages(messages) {
|
|
47
|
+
if (!Array.isArray(messages)) return '';
|
|
48
|
+
const userOnly = messages.filter(m => m && m.role === 'user');
|
|
49
|
+
if (looksLikeCode(messages)) {
|
|
50
|
+
// Код: точное совпадение, чтобы операторы не схлопывались в один ключ.
|
|
51
|
+
return JSON.stringify(userOnly);
|
|
52
|
+
}
|
|
53
|
+
const userTexts = userOnly
|
|
54
|
+
.map(m => {
|
|
55
|
+
if (typeof m.content === 'string') return m.content;
|
|
56
|
+
if (Array.isArray(m.content)) {
|
|
57
|
+
return m.content.filter(c => c && c.type === 'text').map(c => c.text || '').join(' ');
|
|
58
|
+
}
|
|
59
|
+
return '';
|
|
60
|
+
})
|
|
61
|
+
.join('\n');
|
|
62
|
+
return normalizeText(userTexts);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function normalizeText(text) {
|
|
66
|
+
return (text || '')
|
|
67
|
+
.toLowerCase()
|
|
68
|
+
.replace(/[\p{P}\p{S}]+/gu, ' ') // пунктуация и символы → пробел
|
|
69
|
+
.replace(/\s+/g, ' ') // сжать пробелы
|
|
70
|
+
.trim()
|
|
71
|
+
.slice(0, MAX_LEN);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
module.exports = { normalizeMessages, normalizeText, looksLikeCode, MAX_LEN };
|
package/lib/providers.js
CHANGED
|
@@ -69,7 +69,7 @@ for (const [key, provider] of Object.entries(PROVIDERS)) {
|
|
|
69
69
|
const derivedVar = `PROVIDER_${key.toUpperCase().replace(/-/g, '_')}_APIKEY`;
|
|
70
70
|
const prefix = key.split('-')[0].toUpperCase();
|
|
71
71
|
// Map known prefixes to their real env vars
|
|
72
|
-
const prefixMap = { OR: 'OPENROUTER', NIM: 'NIM', GROQ: 'GROQ', MISTRAL: 'MISTRAL', GEMINI: 'GEMINI', ZAI: 'ZAI' };
|
|
72
|
+
const prefixMap = { OR: 'OPENROUTER', NIM: 'NIM', GROQ: 'GROQ', MISTRAL: 'MISTRAL', GEMINI: 'GEMINI', ZAI: 'ZAI', HF: 'HF' };
|
|
73
73
|
const resolvedPrefix = prefixMap[prefix] || prefix;
|
|
74
74
|
const envKey = process.env[explicitVar] || process.env[derivedVar] || process.env[`PROVIDER_${resolvedPrefix}_APIKEY`];
|
|
75
75
|
if (envKey) provider.apiKey = envKey;
|
package/lib/routing.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Классификация сложности запроса. Возвращает 0..1.
|
|
2
|
+
// Признаки кода: ключевые слова, символы, длина сообщения, спец-слова.
|
|
3
|
+
// Внимание: \b в JS учитывает только ASCII-символы, поэтому для русских
|
|
4
|
+
// основ (словоформы склоняются: «функцию», «сортировки») используется
|
|
5
|
+
// префиксное совпадение без границ слова, а для латинских слов — \b.
|
|
6
|
+
const CODE_WORDS_EN = /\b(?:function|class|const|let|var|import|export|return|await|async|def|int|void|throw|try|catch|code)\b/gi;
|
|
7
|
+
const CODE_WORDS_RU = /(?:функци|сортировк|массив|код|переменн|цикл|класс|скрипт|комментари)/gi;
|
|
8
|
+
const CODE_SYMBOLS = /[{}\[\]]|=>/;
|
|
9
|
+
const FIX_WORDS = /(?:ошибк|баг|не работает|рефакторинг|исправь|почини|оптимизир)|\b(?:fix|bug|error|exception|refactor|debug)\b/i;
|
|
10
|
+
const REASONING_WORDS = /(?:объясни|почему|зачем|проанализируй|сравни|докажи|спроектируй|архитектур)/i;
|
|
11
|
+
|
|
12
|
+
const CODE_MATCH_WEIGHT = 0.15;
|
|
13
|
+
const CODE_MATCH_MAX = 0.6;
|
|
14
|
+
|
|
15
|
+
function classifyComplexity(messages) {
|
|
16
|
+
if (!Array.isArray(messages)) return 0;
|
|
17
|
+
// Берем только последнее пользовательское сообщение
|
|
18
|
+
const last = [...messages].reverse().find(m => m && m.role === 'user');
|
|
19
|
+
if (!last) return 0;
|
|
20
|
+
let text = '';
|
|
21
|
+
if (typeof last.content === 'string') text = last.content;
|
|
22
|
+
else if (Array.isArray(last.content)) {
|
|
23
|
+
text = last.content.filter(c => c && c.type === 'text').map(c => c.text || '').join(' ');
|
|
24
|
+
}
|
|
25
|
+
text = (text || '').trim();
|
|
26
|
+
if (text.length === 0) return 0;
|
|
27
|
+
|
|
28
|
+
let score = 0;
|
|
29
|
+
// 1. Длина — длинные запросы сложнее (до +0.3)
|
|
30
|
+
score += Math.min(text.length / 2000, 0.3);
|
|
31
|
+
// 2. Код-признаки (до +0.6)
|
|
32
|
+
let codeMatches = (text.match(CODE_WORDS_EN) || []).length;
|
|
33
|
+
codeMatches += (text.match(CODE_WORDS_RU) || []).length;
|
|
34
|
+
if (CODE_SYMBOLS.test(text)) codeMatches += 1;
|
|
35
|
+
score += Math.min(codeMatches * CODE_MATCH_WEIGHT, CODE_MATCH_MAX);
|
|
36
|
+
// 3. Слова об исправлении/ошибках (до +0.2)
|
|
37
|
+
if (FIX_WORDS.test(text)) score += 0.2;
|
|
38
|
+
// 4. Слова о рассуждениях (до +0.2)
|
|
39
|
+
if (REASONING_WORDS.test(text)) score += 0.2;
|
|
40
|
+
// 5. Наличие блоков кода ``` (до +0.2)
|
|
41
|
+
if ((text.match(/```/g) || []).length >= 2) score += 0.2;
|
|
42
|
+
return Math.min(score, 1);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Поднятие тира при высокой сложности. Простые остаются на месте.
|
|
46
|
+
const UPGRADE_MAP = {
|
|
47
|
+
'tier-s': 'tier-splus', // лёгкий → мощный (ox-alpha reasoning)
|
|
48
|
+
'tier-a': 'tier-s', // лёгкий → быстрый
|
|
49
|
+
'tier-b': 'tier-s', // лёгкий → быстрый
|
|
50
|
+
};
|
|
51
|
+
const COMPLEX_THRESHOLD = 0.5;
|
|
52
|
+
|
|
53
|
+
function maybeUpgradeTier(requestedModel, complexity) {
|
|
54
|
+
if (complexity >= COMPLEX_THRESHOLD && UPGRADE_MAP[requestedModel]) {
|
|
55
|
+
return UPGRADE_MAP[requestedModel];
|
|
56
|
+
}
|
|
57
|
+
return requestedModel;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports = { classifyComplexity, maybeUpgradeTier, COMPLEX_THRESHOLD };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "freegate",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.8",
|
|
4
4
|
"description": "Free multi-provider LLM gateway with automatic failover. One OpenAI-compatible endpoint routes to 25 free models (Groq, Mistral, Gemini, NIM, OpenRouter, ZAI, Cerebras, DeepSeek). Never pay for LLMs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"bin": {
|
package/providers.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"model": "openai/gpt-oss-120b",
|
|
5
5
|
"priority": 1,
|
|
6
6
|
"dailyLimit": 1000,
|
|
7
|
-
"keyHint": "console.groq.com
|
|
7
|
+
"keyHint": "console.groq.com → API Keys",
|
|
8
8
|
"envVar": "PROVIDER_GROQ_APIKEY",
|
|
9
9
|
"free": true,
|
|
10
10
|
"category": "general"
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"model": "qwen/qwen3.6-27b",
|
|
15
15
|
"priority": 2,
|
|
16
16
|
"dailyLimit": 1000,
|
|
17
|
-
"keyHint": "console.groq.com
|
|
17
|
+
"keyHint": "console.groq.com → API Keys",
|
|
18
18
|
"envVar": "PROVIDER_GROQ_APIKEY",
|
|
19
19
|
"free": true,
|
|
20
20
|
"category": "general"
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"model": "allam-2-7b",
|
|
25
25
|
"priority": 3,
|
|
26
26
|
"dailyLimit": 1000,
|
|
27
|
-
"keyHint": "console.groq.com
|
|
27
|
+
"keyHint": "console.groq.com → API Keys (allam-2-7b, очень быстрая 229ms)",
|
|
28
28
|
"envVar": "PROVIDER_GROQ_APIKEY",
|
|
29
29
|
"free": true,
|
|
30
30
|
"category": "general"
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"model": "groq/compound",
|
|
35
35
|
"priority": 12,
|
|
36
36
|
"dailyLimit": 1000,
|
|
37
|
-
"keyHint": "console.groq.com
|
|
37
|
+
"keyHint": "console.groq.com → API Keys (compound)",
|
|
38
38
|
"envVar": "PROVIDER_GROQ_APIKEY",
|
|
39
39
|
"free": true,
|
|
40
40
|
"category": "general"
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"model": "codestral-latest",
|
|
45
45
|
"priority": 3,
|
|
46
46
|
"dailyLimit": 500000,
|
|
47
|
-
"keyHint": "console.mistral.ai
|
|
47
|
+
"keyHint": "console.mistral.ai → API Keys",
|
|
48
48
|
"envVar": "PROVIDER_MISTRAL_APIKEY",
|
|
49
49
|
"free": true,
|
|
50
50
|
"category": "coding"
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"model": "mistral-small-latest",
|
|
55
55
|
"priority": 4,
|
|
56
56
|
"dailyLimit": 500000,
|
|
57
|
-
"keyHint": "console.mistral.ai
|
|
57
|
+
"keyHint": "console.mistral.ai → API Keys",
|
|
58
58
|
"envVar": "PROVIDER_MISTRAL_APIKEY",
|
|
59
59
|
"free": true,
|
|
60
60
|
"category": "general"
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"model": "gemini-3.6-flash",
|
|
65
65
|
"priority": 5,
|
|
66
66
|
"dailyLimit": 1500,
|
|
67
|
-
"keyHint": "aistudio.google.com
|
|
67
|
+
"keyHint": "aistudio.google.com → Get API key",
|
|
68
68
|
"envVar": "PROVIDER_GEMINI_APIKEY",
|
|
69
69
|
"free": true,
|
|
70
70
|
"category": "general"
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"model": "deepseek-ai/deepseek-v4-flash-0731",
|
|
75
75
|
"priority": 6,
|
|
76
76
|
"dailyLimit": 40,
|
|
77
|
-
"keyHint": "build.nvidia.com
|
|
77
|
+
"keyHint": "build.nvidia.com → Get API key",
|
|
78
78
|
"envVar": "PROVIDER_NIM_APIKEY",
|
|
79
79
|
"free": true,
|
|
80
80
|
"category": "general"
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
"model": "meta/llama-3.1-8b-instruct",
|
|
85
85
|
"priority": 7,
|
|
86
86
|
"dailyLimit": 40,
|
|
87
|
-
"keyHint": "build.nvidia.com
|
|
87
|
+
"keyHint": "build.nvidia.com → Get API key",
|
|
88
88
|
"envVar": "PROVIDER_NIM_APIKEY",
|
|
89
89
|
"free": true,
|
|
90
90
|
"category": "general"
|
|
@@ -94,7 +94,7 @@
|
|
|
94
94
|
"model": "glm-4.7-flash",
|
|
95
95
|
"priority": 8,
|
|
96
96
|
"dailyLimit": 1000,
|
|
97
|
-
"keyHint": "open.bigmodel.cn
|
|
97
|
+
"keyHint": "open.bigmodel.cn → API Keys",
|
|
98
98
|
"envVar": "PROVIDER_ZAI_APIKEY",
|
|
99
99
|
"free": true,
|
|
100
100
|
"category": "general"
|
|
@@ -104,7 +104,7 @@
|
|
|
104
104
|
"model": "cohere/north-mini-code:free",
|
|
105
105
|
"priority": 17,
|
|
106
106
|
"dailyLimit": 50,
|
|
107
|
-
"keyHint": "openrouter.ai
|
|
107
|
+
"keyHint": "openrouter.ai → Keys (медленный, запасной)",
|
|
108
108
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
109
109
|
"free": true,
|
|
110
110
|
"category": "coding"
|
|
@@ -114,7 +114,7 @@
|
|
|
114
114
|
"model": "z-ai/glm-5.2:free",
|
|
115
115
|
"priority": 10,
|
|
116
116
|
"dailyLimit": 50,
|
|
117
|
-
"keyHint": "openrouter.ai
|
|
117
|
+
"keyHint": "openrouter.ai → Keys",
|
|
118
118
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
119
119
|
"free": true,
|
|
120
120
|
"category": "general"
|
|
@@ -124,7 +124,7 @@
|
|
|
124
124
|
"model": "nvidia/nemotron-3-ultra-550b-a55b:free",
|
|
125
125
|
"priority": 11,
|
|
126
126
|
"dailyLimit": 50,
|
|
127
|
-
"keyHint": "openrouter.ai
|
|
127
|
+
"keyHint": "openrouter.ai → Keys",
|
|
128
128
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
129
129
|
"free": true,
|
|
130
130
|
"category": "reasoning"
|
|
@@ -134,7 +134,7 @@
|
|
|
134
134
|
"model": "nvidia/nemotron-3-super-120b-a12b:free",
|
|
135
135
|
"priority": 12,
|
|
136
136
|
"dailyLimit": 50,
|
|
137
|
-
"keyHint": "openrouter.ai
|
|
137
|
+
"keyHint": "openrouter.ai → Keys",
|
|
138
138
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
139
139
|
"free": true,
|
|
140
140
|
"category": "reasoning"
|
|
@@ -144,7 +144,7 @@
|
|
|
144
144
|
"model": "openai/gpt-oss-20b:free",
|
|
145
145
|
"priority": 13,
|
|
146
146
|
"dailyLimit": 50,
|
|
147
|
-
"keyHint": "openrouter.ai
|
|
147
|
+
"keyHint": "openrouter.ai → Keys",
|
|
148
148
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
149
149
|
"free": true,
|
|
150
150
|
"category": "general"
|
|
@@ -154,7 +154,7 @@
|
|
|
154
154
|
"model": "stealth/ox-alpha",
|
|
155
155
|
"priority": 3,
|
|
156
156
|
"dailyLimit": 100,
|
|
157
|
-
"keyHint": "openrouter.ai
|
|
157
|
+
"keyHint": "openrouter.ai → Keys (Ox Alpha: 1M контекст, reasoning, бесплатно)",
|
|
158
158
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
159
159
|
"free": true,
|
|
160
160
|
"reasoning": true,
|
|
@@ -165,7 +165,7 @@
|
|
|
165
165
|
"model": "nvidia/nemotron-3.5-lightning:free",
|
|
166
166
|
"priority": 14,
|
|
167
167
|
"dailyLimit": 100,
|
|
168
|
-
"keyHint": "openrouter.ai
|
|
168
|
+
"keyHint": "openrouter.ai → Keys (Nemotron 3.5 Lightning: 977K контекст)",
|
|
169
169
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
170
170
|
"free": true,
|
|
171
171
|
"category": "reasoning"
|
|
@@ -175,7 +175,7 @@
|
|
|
175
175
|
"model": "poolside/laguna-s-2.1:free",
|
|
176
176
|
"priority": 15,
|
|
177
177
|
"dailyLimit": 100,
|
|
178
|
-
"keyHint": "openrouter.ai
|
|
178
|
+
"keyHint": "openrouter.ai → Keys (Laguna S: кодинг, 118B)",
|
|
179
179
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
180
180
|
"free": true,
|
|
181
181
|
"category": "coding"
|
|
@@ -185,7 +185,7 @@
|
|
|
185
185
|
"model": "gemini-3.6-flash",
|
|
186
186
|
"priority": 5,
|
|
187
187
|
"dailyLimit": 1500,
|
|
188
|
-
"keyHint": "aistudio.google.com
|
|
188
|
+
"keyHint": "aistudio.google.com → Get API key (vision: работает со скриншотами)",
|
|
189
189
|
"envVar": "PROVIDER_GEMINI_APIKEY",
|
|
190
190
|
"free": true,
|
|
191
191
|
"vision": true,
|
|
@@ -196,7 +196,7 @@
|
|
|
196
196
|
"model": "meta/llama-3.2-11b-vision-instruct",
|
|
197
197
|
"priority": 18,
|
|
198
198
|
"dailyLimit": 40,
|
|
199
|
-
"keyHint": "build.nvidia.com
|
|
199
|
+
"keyHint": "build.nvidia.com → Get API key (vision, медленный, запасной)",
|
|
200
200
|
"envVar": "PROVIDER_NIM_APIKEY",
|
|
201
201
|
"free": true,
|
|
202
202
|
"vision": true,
|
|
@@ -207,7 +207,7 @@
|
|
|
207
207
|
"model": "qwen2.5:0.5b",
|
|
208
208
|
"priority": 17,
|
|
209
209
|
"dailyLimit": 100000,
|
|
210
|
-
"keyHint": "
|
|
210
|
+
"keyHint": "Локальная модель. Установи Ollama и скачай модель: ollama pull qwen2.5:0.5b (без ключа, безлимит, приватно)",
|
|
211
211
|
"envVar": "",
|
|
212
212
|
"free": true,
|
|
213
213
|
"local": true,
|
|
@@ -218,7 +218,7 @@
|
|
|
218
218
|
"model": "local-model",
|
|
219
219
|
"priority": 18,
|
|
220
220
|
"dailyLimit": 100000,
|
|
221
|
-
"keyHint": "
|
|
221
|
+
"keyHint": "Локальная модель. Установи LM Studio и загрузи модель (без ключа, безлимит)",
|
|
222
222
|
"envVar": "",
|
|
223
223
|
"free": true,
|
|
224
224
|
"local": true,
|
|
@@ -229,7 +229,7 @@
|
|
|
229
229
|
"model": "gpt-oss-120b",
|
|
230
230
|
"priority": 2,
|
|
231
231
|
"dailyLimit": 1000,
|
|
232
|
-
"keyHint": "cloud.cerebras.ai
|
|
232
|
+
"keyHint": "cloud.cerebras.ai → API Keys (gpt-oss-120b, очень быстрая)",
|
|
233
233
|
"envVar": "PROVIDER_CEREBRAS_APIKEY",
|
|
234
234
|
"free": true,
|
|
235
235
|
"category": "general"
|
|
@@ -239,7 +239,7 @@
|
|
|
239
239
|
"model": "gemma-4-31b",
|
|
240
240
|
"priority": 8,
|
|
241
241
|
"dailyLimit": 1000,
|
|
242
|
-
"keyHint": "cloud.cerebras.ai
|
|
242
|
+
"keyHint": "cloud.cerebras.ai → API Keys (gemma-4-31b)",
|
|
243
243
|
"envVar": "PROVIDER_CEREBRAS_APIKEY",
|
|
244
244
|
"free": true,
|
|
245
245
|
"category": "general"
|
|
@@ -249,7 +249,7 @@
|
|
|
249
249
|
"model": "deepseek-v4-flash",
|
|
250
250
|
"priority": 4,
|
|
251
251
|
"dailyLimit": 1000,
|
|
252
|
-
"keyHint": "platform.deepseek.com
|
|
252
|
+
"keyHint": "platform.deepseek.com → API Keys (deepseek-v4-flash)",
|
|
253
253
|
"envVar": "PROVIDER_DEEPSEEK_APIKEY",
|
|
254
254
|
"free": true,
|
|
255
255
|
"category": "general"
|
|
@@ -259,7 +259,7 @@
|
|
|
259
259
|
"model": "deepseek-v4-flash-vision-exp",
|
|
260
260
|
"priority": 14,
|
|
261
261
|
"dailyLimit": 1000,
|
|
262
|
-
"keyHint": "platform.deepseek.com
|
|
262
|
+
"keyHint": "platform.deepseek.com → API Keys (vision)",
|
|
263
263
|
"envVar": "PROVIDER_DEEPSEEK_APIKEY",
|
|
264
264
|
"free": true,
|
|
265
265
|
"vision": true,
|
|
@@ -270,7 +270,7 @@
|
|
|
270
270
|
"model": "dots-studio/dots-3-note-preview:free",
|
|
271
271
|
"priority": 19,
|
|
272
272
|
"dailyLimit": 50,
|
|
273
|
-
"keyHint": "openrouter.ai
|
|
273
|
+
"keyHint": "openrouter.ai → Keys (Dots 3: 280B MoE, мощная)",
|
|
274
274
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
275
275
|
"free": true,
|
|
276
276
|
"category": "reasoning"
|
|
@@ -280,7 +280,7 @@
|
|
|
280
280
|
"model": "liquid/lfm-2.5-2.6b:free",
|
|
281
281
|
"priority": 20,
|
|
282
282
|
"dailyLimit": 50,
|
|
283
|
-
"keyHint": "openrouter.ai
|
|
283
|
+
"keyHint": "openrouter.ai → Keys (LFM 2.5: быстрая reasoning)",
|
|
284
284
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
285
285
|
"free": true,
|
|
286
286
|
"category": "reasoning"
|
|
@@ -290,9 +290,19 @@
|
|
|
290
290
|
"model": "poolside/laguna-xs-2.1:free",
|
|
291
291
|
"priority": 21,
|
|
292
292
|
"dailyLimit": 50,
|
|
293
|
-
"keyHint": "openrouter.ai
|
|
293
|
+
"keyHint": "openrouter.ai → Keys (Laguna XS: кодинг)",
|
|
294
294
|
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
295
295
|
"free": true,
|
|
296
296
|
"category": "coding"
|
|
297
|
+
},
|
|
298
|
+
"or-nemotron-3-nano-omni-30b-a3b-r": {
|
|
299
|
+
"endpoint": "https://openrouter.ai/api/v1/chat/completions",
|
|
300
|
+
"model": "nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free",
|
|
301
|
+
"priority": 20,
|
|
302
|
+
"dailyLimit": 50,
|
|
303
|
+
"keyHint": "openrouter.ai → Keys (автодобавлено, vision)",
|
|
304
|
+
"envVar": "PROVIDER_OPENROUTER_APIKEY",
|
|
305
|
+
"free": true,
|
|
306
|
+
"category": "vision"
|
|
297
307
|
}
|
|
298
|
-
}
|
|
308
|
+
}
|
package/server.js
CHANGED
|
@@ -9,6 +9,7 @@ const { checkRateLimit } = require('./lib/rateLimit');
|
|
|
9
9
|
const { handleDashboard } = require('./lib/dashboard');
|
|
10
10
|
const { acquire, stats: poolStats } = require('./lib/pool');
|
|
11
11
|
const { stripThink, cleanDelta, cleanMessage, fixReasoningMessage } = require('./lib/clean');
|
|
12
|
+
const { classifyComplexity, maybeUpgradeTier } = require('./lib/routing');
|
|
12
13
|
const logger = require('./lib/logger');
|
|
13
14
|
|
|
14
15
|
// Load persisted state
|
|
@@ -24,7 +25,7 @@ if (stale.length > 0) {
|
|
|
24
25
|
logger.info('Cleaned stale health entries', { removed: stale });
|
|
25
26
|
}
|
|
26
27
|
|
|
27
|
-
const cache = new LRUCache(500, 3600000);
|
|
28
|
+
const cache = new LRUCache(500, 3600000, false, true); // 4th arg: semantic normalize ON
|
|
28
29
|
require('./lib/cache')._activeCache = cache;
|
|
29
30
|
|
|
30
31
|
// Load config (with fallback so a corrupt config never crashes the server)
|
|
@@ -120,7 +121,10 @@ setTimeout(healthCheck, 1000);
|
|
|
120
121
|
// Chat completion handler
|
|
121
122
|
async function handleChatCompletion(req, res, body) {
|
|
122
123
|
const requestedModel = body.model || 'tier-splus';
|
|
123
|
-
|
|
124
|
+
// Умный роутинг: сложные задачи с лёгкого тира поднимаем на более мощный.
|
|
125
|
+
// Классифицируем ПОСЛЕ того, как определён requestedModel, ДО выбора провайдера.
|
|
126
|
+
const effectiveModel = maybeUpgradeTier(requestedModel, classifyComplexity(body.messages));
|
|
127
|
+
let targetProviderKey = MODEL_MAP[effectiveModel] || MODEL_MAP[requestedModel] || 'zai';
|
|
124
128
|
const isStreaming = body.stream === true;
|
|
125
129
|
|
|
126
130
|
// Vision detection: if the request contains images, route to a vision provider.
|
|
@@ -205,7 +209,7 @@ async function handleChatCompletion(req, res, body) {
|
|
|
205
209
|
}
|
|
206
210
|
|
|
207
211
|
// Check cache (works for both streaming and non-streaming)
|
|
208
|
-
const cached = cache.get(
|
|
212
|
+
const cached = cache.get(effectiveModel, body.messages, body.temperature);
|
|
209
213
|
if (cached) {
|
|
210
214
|
logger.request({ model: requestedModel, provider: 'cache', status: 200, cached: true });
|
|
211
215
|
recordRecent({ model: requestedModel, provider: 'cache', status: 200, latency: 0, cached: true });
|
|
@@ -257,7 +261,10 @@ async function handleChatCompletion(req, res, body) {
|
|
|
257
261
|
let weight = score / lat;
|
|
258
262
|
// Rate-limited providers are last resort — heavy penalty
|
|
259
263
|
if (h.status === 'ratelimited') weight *= 0.05;
|
|
260
|
-
|
|
264
|
+
// Mapped (target) provider gets a SLIGHT preference, but the pool must
|
|
265
|
+
// still be able to pick faster/healthier providers for tier aliases —
|
|
266
|
+
// otherwise Freegate always routes tier-s to Codestral and never varies.
|
|
267
|
+
if (key === targetProviderKey) weight *= 1.15;
|
|
261
268
|
const dailyLimit = provider.dailyLimit || 1000;
|
|
262
269
|
const usedToday = getStats().providerUsage[key] || 0;
|
|
263
270
|
if (usedToday >= dailyLimit * 0.9) weight *= 0.5;
|
|
@@ -279,31 +286,35 @@ async function handleChatCompletion(req, res, body) {
|
|
|
279
286
|
}).sort((a, b) => b.weight - a.weight);
|
|
280
287
|
|
|
281
288
|
const totalWeight = scored.reduce((s, p) => s + p.weight, 0);
|
|
289
|
+
// Weighted random: pick ONE provider as the starting candidate. The rest
|
|
290
|
+
// are kept as fallbacks below. (Bugfix: was assigning the whole `scored`
|
|
291
|
+
// list, which made the first (heaviest) provider win every time.)
|
|
282
292
|
let r = Math.random() * totalWeight;
|
|
283
293
|
for (const p of scored) {
|
|
284
294
|
r -= p.weight;
|
|
285
|
-
if (r <= 0) { selected =
|
|
295
|
+
if (r <= 0) { selected = [p]; break; }
|
|
286
296
|
}
|
|
287
|
-
if (selected.length === 0) selected = scored;
|
|
297
|
+
if (selected.length === 0) selected = [scored[0]];
|
|
288
298
|
}
|
|
289
299
|
|
|
290
|
-
|
|
291
|
-
|
|
300
|
+
// Weighted-random picked ONE provider as the primary; append the rest of the
|
|
301
|
+
// healthy pool (by weight) as fallbacks so a failing pick still recovers.
|
|
302
|
+
const restOfPool = selected.length > 0 && pool.length > 0
|
|
303
|
+
? pool.map(([k, p]) => ({ key: k, provider: p })).filter(s => s.key !== selected[0].key)
|
|
304
|
+
.sort((a, b) => (getHealth()[b.key]?.score || 0) - (getHealth()[a.key]?.score || 0))
|
|
305
|
+
: [];
|
|
306
|
+
const enabledProviders = selected.length > 0
|
|
307
|
+
? [selected[0]].concat(restOfPool).map(s => [s.key, s.provider])
|
|
308
|
+
: Object.entries(PROVIDERS).filter(([_, p]) => p.enabled)
|
|
292
309
|
.sort((a, b) => (getHealth()[b[0]]?.score || 50) - (getHealth()[a[0]]?.score || 50));
|
|
293
310
|
|
|
294
|
-
//
|
|
295
|
-
//
|
|
296
|
-
//
|
|
311
|
+
// Ensure the requested model's mapped provider is at least IN the candidate
|
|
312
|
+
// list (it may have been filtered out), but DON'T force it to the front —
|
|
313
|
+
// the weighted selection above should pick the fastest/healthiest provider.
|
|
297
314
|
if (MODEL_MAP[requestedModel] && PROVIDERS[targetProviderKey]) {
|
|
298
|
-
// Ensure the target provider is in the candidate list at all
|
|
299
315
|
if (!enabledProviders.some(([k]) => k === targetProviderKey)) {
|
|
300
316
|
enabledProviders.unshift([targetProviderKey, PROVIDERS[targetProviderKey]]);
|
|
301
317
|
}
|
|
302
|
-
const targetIdx = enabledProviders.findIndex(([k]) => k === targetProviderKey);
|
|
303
|
-
if (targetIdx > 0) {
|
|
304
|
-
const [t] = enabledProviders.splice(targetIdx, 1);
|
|
305
|
-
enabledProviders.unshift(t);
|
|
306
|
-
}
|
|
307
318
|
}
|
|
308
319
|
|
|
309
320
|
if (enabledProviders.length === 0) {
|
|
@@ -380,7 +391,7 @@ async function handleChatCompletion(req, res, body) {
|
|
|
380
391
|
// Cache the assembled answer for repeat prompts (only if complete)
|
|
381
392
|
if (chunks.length > 0) {
|
|
382
393
|
const full = chunks.join('');
|
|
383
|
-
cache.set(
|
|
394
|
+
cache.set(effectiveModel, body.messages, body.temperature, {
|
|
384
395
|
id: 'chatcmpl-cached',
|
|
385
396
|
object: 'chat.completion',
|
|
386
397
|
created: Math.floor(Date.now() / 1000),
|
|
@@ -404,7 +415,7 @@ async function handleChatCompletion(req, res, body) {
|
|
|
404
415
|
fixReasoningMessage(result.data.choices[0].message);
|
|
405
416
|
cleanMessage(result.data.choices[0].message);
|
|
406
417
|
}
|
|
407
|
-
cache.set(
|
|
418
|
+
cache.set(effectiveModel, body.messages, body.temperature, result.data);
|
|
408
419
|
recordTokens(key, result.usage);
|
|
409
420
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
410
421
|
res.end(JSON.stringify(result.data));
|
|
@@ -465,7 +476,7 @@ async function handleChatCompletion(req, res, body) {
|
|
|
465
476
|
fixReasoningMessage(result.data.choices[0].message);
|
|
466
477
|
cleanMessage(result.data.choices[0].message);
|
|
467
478
|
}
|
|
468
|
-
cache.set(
|
|
479
|
+
cache.set(effectiveModel, body.messages, body.temperature, result.data);
|
|
469
480
|
recordTokens(key, result.usage);
|
|
470
481
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
471
482
|
res.end(JSON.stringify(result.data));
|