freegate 0.6.8 → 0.6.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "freegate",
3
- "version": "0.6.8",
3
+ "version": "0.6.9",
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/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));