freegate 0.6.8 → 0.6.10

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/clean.js CHANGED
@@ -53,4 +53,16 @@ function cleanDelta(delta) {
53
53
  return delta;
54
54
  }
55
55
 
56
- module.exports = { stripThink, cleanMessage, cleanDelta, fixReasoningMessage };
56
+ // True if a non-streaming completion actually contains answer text.
57
+ // Used before caching — empty responses (provider glitch) must NOT be cached,
58
+ // otherwise every similar request returns the empty answer forever.
59
+ function hasContent(data) {
60
+ if (!data || !Array.isArray(data.choices)) return false;
61
+ const msg = data.choices[0]?.message;
62
+ if (!msg) return false;
63
+ const content = typeof msg.content === 'string' ? msg.content.trim() : '';
64
+ const reasoning = typeof msg.reasoning === 'string' ? msg.reasoning.trim() : '';
65
+ return content.length > 0 || reasoning.length > 0;
66
+ }
67
+
68
+ module.exports = { stripThink, cleanMessage, cleanDelta, fixReasoningMessage, hasContent };
package/lib/normalize.js CHANGED
@@ -45,21 +45,36 @@ function looksLikeCode(messages) {
45
45
 
46
46
  function normalizeMessages(messages) {
47
47
  if (!Array.isArray(messages)) return '';
48
- const userOnly = messages.filter(m => m && m.role === 'user');
48
+ const dialog = messages.filter(m => m && (m.role === 'user' || m.role === 'assistant'));
49
49
  if (looksLikeCode(messages)) {
50
50
  // Код: точное совпадение, чтобы операторы не схлопывались в один ключ.
51
- return JSON.stringify(userOnly);
51
+ return JSON.stringify(dialog);
52
52
  }
53
- const userTexts = userOnly
53
+ // Включаем ВЕСЬ диалог (user + assistant), но НЕ system-промпт: короткие
54
+ // команды («продолжай») из разных диалогов с разным контекстом не должны
55
+ // схлопываться в один кэш-ключ и возвращать чужой ответ.
56
+ const texts = dialog
54
57
  .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
+ const role = m.role === 'user' ? 'u' : 'a';
59
+ let body = '';
60
+ if (typeof m.content === 'string') body = m.content;
61
+ else if (Array.isArray(m.content)) {
62
+ body = m.content.filter(c => c && c.type === 'text').map(c => c.text || '').join(' ');
58
63
  }
64
+ return role + ':' + body;
65
+ })
66
+ .join('\n');
67
+ // Нет извлекаемого текста (только картинки/инструменты) → пусто, чтобы cache
68
+ // упал на точное совпадение (иначе image-only запросы схлопнутся в один ключ).
69
+ const rawText = dialog
70
+ .map(m => {
71
+ if (typeof m.content === 'string') return m.content;
72
+ if (Array.isArray(m.content)) return m.content.filter(c => c && c.type === 'text').map(c => c.text || '').join(' ');
59
73
  return '';
60
74
  })
61
75
  .join('\n');
62
- return normalizeText(userTexts);
76
+ if (!rawText.trim()) return '';
77
+ return normalizeText(texts);
63
78
  }
64
79
 
65
80
  function normalizeText(text) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "freegate",
3
- "version": "0.6.8",
3
+ "version": "0.6.10",
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
@@ -304,5 +304,75 @@
304
304
  "envVar": "PROVIDER_OPENROUTER_APIKEY",
305
305
  "free": true,
306
306
  "category": "vision"
307
+ },
308
+ "hf-Qwen3-8-2-4T-A95B": {
309
+ "endpoint": "https://router.huggingface.co/v1/chat/completions",
310
+ "model": "Qwen/Qwen3.8-2.4T-A95B",
311
+ "priority": 20,
312
+ "dailyLimit": 200,
313
+ "keyHint": "huggingface.co → Settings → Tokens (автодобавлено, general)",
314
+ "envVar": "PROVIDER_HF_APIKEY",
315
+ "free": true,
316
+ "category": "general"
317
+ },
318
+ "hf-GLM-5-2": {
319
+ "endpoint": "https://router.huggingface.co/v1/chat/completions",
320
+ "model": "zai-org/GLM-5.2",
321
+ "priority": 20,
322
+ "dailyLimit": 200,
323
+ "keyHint": "huggingface.co → Settings → Tokens (автодобавлено, general)",
324
+ "envVar": "PROVIDER_HF_APIKEY",
325
+ "free": true,
326
+ "category": "general"
327
+ },
328
+ "hf-gemma-4-31B-it": {
329
+ "endpoint": "https://router.huggingface.co/v1/chat/completions",
330
+ "model": "google/gemma-4-31B-it",
331
+ "priority": 20,
332
+ "dailyLimit": 200,
333
+ "keyHint": "huggingface.co → Settings → Tokens (автодобавлено, general)",
334
+ "envVar": "PROVIDER_HF_APIKEY",
335
+ "free": true,
336
+ "category": "general"
337
+ },
338
+ "hf-DeepSeek-V4-Pro": {
339
+ "endpoint": "https://router.huggingface.co/v1/chat/completions",
340
+ "model": "deepseek-ai/DeepSeek-V4-Pro",
341
+ "priority": 20,
342
+ "dailyLimit": 200,
343
+ "keyHint": "huggingface.co → Settings → Tokens (автодобавлено, general)",
344
+ "envVar": "PROVIDER_HF_APIKEY",
345
+ "free": true,
346
+ "category": "general"
347
+ },
348
+ "hf-gemma-4-26B-A4B-it": {
349
+ "endpoint": "https://router.huggingface.co/v1/chat/completions",
350
+ "model": "google/gemma-4-26B-A4B-it",
351
+ "priority": 20,
352
+ "dailyLimit": 200,
353
+ "keyHint": "huggingface.co → Settings → Tokens (автодобавлено, general)",
354
+ "envVar": "PROVIDER_HF_APIKEY",
355
+ "free": true,
356
+ "category": "general"
357
+ },
358
+ "hf-Llama-3-1-8B-Instruct": {
359
+ "endpoint": "https://router.huggingface.co/v1/chat/completions",
360
+ "model": "meta-llama/Llama-3.1-8B-Instruct",
361
+ "priority": 20,
362
+ "dailyLimit": 200,
363
+ "keyHint": "huggingface.co → Settings → Tokens (автодобавлено, general)",
364
+ "envVar": "PROVIDER_HF_APIKEY",
365
+ "free": true,
366
+ "category": "general"
367
+ },
368
+ "hf-DeepSeek-V4-Flash": {
369
+ "endpoint": "https://router.huggingface.co/v1/chat/completions",
370
+ "model": "deepseek-ai/DeepSeek-V4-Flash",
371
+ "priority": 20,
372
+ "dailyLimit": 200,
373
+ "keyHint": "huggingface.co → Settings → Tokens (автодобавлено, general)",
374
+ "envVar": "PROVIDER_HF_APIKEY",
375
+ "free": true,
376
+ "category": "general"
307
377
  }
308
378
  }
package/server.js CHANGED
@@ -8,7 +8,7 @@ const { loadState, initHealth, isCircuitOpen, recordSuccess, recordFailure, reco
8
8
  const { checkRateLimit } = require('./lib/rateLimit');
9
9
  const { handleDashboard } = require('./lib/dashboard');
10
10
  const { acquire, stats: poolStats } = require('./lib/pool');
11
- const { stripThink, cleanDelta, cleanMessage, fixReasoningMessage } = require('./lib/clean');
11
+ const { stripThink, cleanDelta, cleanMessage, fixReasoningMessage, hasContent } = require('./lib/clean');
12
12
  const { classifyComplexity, maybeUpgradeTier } = require('./lib/routing');
13
13
  const logger = require('./lib/logger');
14
14
 
@@ -388,9 +388,11 @@ async function handleChatCompletion(req, res, body) {
388
388
  }
389
389
  });
390
390
  result.stream.on('end', () => {
391
- // Cache the assembled answer for repeat prompts (only if complete)
391
+ // Cache the assembled answer for repeat prompts (only if complete
392
+ // AND non-empty — empty answers must not be cached).
392
393
  if (chunks.length > 0) {
393
394
  const full = chunks.join('');
395
+ if (full.trim().length > 0) {
394
396
  cache.set(effectiveModel, body.messages, body.temperature, {
395
397
  id: 'chatcmpl-cached',
396
398
  object: 'chat.completion',
@@ -399,6 +401,7 @@ async function handleChatCompletion(req, res, body) {
399
401
  choices: [{ index: 0, message: { role: 'assistant', content: full }, finish_reason: 'stop' }],
400
402
  usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
401
403
  });
404
+ }
402
405
  }
403
406
  });
404
407
  result.stream.on('error', (err) => {
@@ -415,7 +418,7 @@ async function handleChatCompletion(req, res, body) {
415
418
  fixReasoningMessage(result.data.choices[0].message);
416
419
  cleanMessage(result.data.choices[0].message);
417
420
  }
418
- cache.set(effectiveModel, body.messages, body.temperature, result.data);
421
+ if (hasContent(result.data)) cache.set(effectiveModel, body.messages, body.temperature, result.data);
419
422
  recordTokens(key, result.usage);
420
423
  res.writeHead(200, { 'Content-Type': 'application/json' });
421
424
  res.end(JSON.stringify(result.data));
@@ -476,7 +479,7 @@ async function handleChatCompletion(req, res, body) {
476
479
  fixReasoningMessage(result.data.choices[0].message);
477
480
  cleanMessage(result.data.choices[0].message);
478
481
  }
479
- cache.set(effectiveModel, body.messages, body.temperature, result.data);
482
+ if (hasContent(result.data)) cache.set(effectiveModel, body.messages, body.temperature, result.data);
480
483
  recordTokens(key, result.usage);
481
484
  res.writeHead(200, { 'Content-Type': 'application/json' });
482
485
  res.end(JSON.stringify(result.data));