freellm 1.0.13 → 1.0.15

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.
@@ -8,7 +8,7 @@
8
8
  * { box-sizing: border-box; margin: 0; padding: 0; }
9
9
  body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: #0f0f0f; color: #e1e1e1; }
10
10
  </style>
11
- <script type="module" crossorigin src="/assets/index-CZTExjJU.js"></script>
11
+ <script type="module" crossorigin src="/assets/index-DyoXu6aO.js"></script>
12
12
  </head>
13
13
  <body>
14
14
  <div id="root"></div>
package/dist/cli.js CHANGED
@@ -1,10 +1,80 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  const index_1 = require("./index");
7
+ const path_1 = __importDefault(require("path"));
8
+ const child_process_1 = require("child_process");
9
+ const fs_1 = require("fs");
4
10
  const args = process.argv.slice(2);
5
- const port = args.includes('--port') ? Number(args[args.indexOf('--port') + 1]) : Number(process.env.PORT ?? 3000);
6
- const noOpen = args.includes('--no-open');
11
+ function getCurrentVersion() {
12
+ try {
13
+ const pkg = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(__dirname, '..', 'package.json'), 'utf8'));
14
+ return pkg.version;
15
+ }
16
+ catch {
17
+ return '0.0.0';
18
+ }
19
+ }
20
+ async function getLatestVersion() {
21
+ try {
22
+ const res = await fetch('https://registry.npmjs.org/freellm/latest', {
23
+ signal: AbortSignal.timeout(10000),
24
+ });
25
+ if (!res.ok)
26
+ return null;
27
+ const data = (await res.json());
28
+ return data.version;
29
+ }
30
+ catch {
31
+ return null;
32
+ }
33
+ }
34
+ function compareVersions(a, b) {
35
+ const pa = a.split('.').map(Number);
36
+ const pb = b.split('.').map(Number);
37
+ for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
38
+ const x = pa[i] ?? 0;
39
+ const y = pb[i] ?? 0;
40
+ if (x > y)
41
+ return 1;
42
+ if (x < y)
43
+ return -1;
44
+ }
45
+ return 0;
46
+ }
47
+ async function update() {
48
+ console.log('');
49
+ console.log(' FreeLLM — checking for updates...');
50
+ const current = getCurrentVersion();
51
+ const latest = await getLatestVersion();
52
+ if (!latest) {
53
+ console.log(' Could not reach the npm registry. Check your connection and try again.');
54
+ process.exit(1);
55
+ }
56
+ if (compareVersions(current, latest) >= 0) {
57
+ console.log(` You're already on the latest version (${current}).`);
58
+ return;
59
+ }
60
+ console.log(` Updating ${current} → ${latest}...`);
61
+ const result = (0, child_process_1.spawnSync)('npm', ['install', '-g', `freellm@${latest}`], {
62
+ stdio: 'inherit',
63
+ shell: process.platform === 'win32',
64
+ });
65
+ if (result.status !== 0) {
66
+ console.error(` Update failed. Try running: npm install -g freellm@${latest}`);
67
+ process.exit(1);
68
+ }
69
+ console.log(` Updated to FreeLLM ${latest}. Run "freellm" to start.`);
70
+ }
7
71
  async function main() {
72
+ if (args[0] === 'update' || args[0] === 'upgrade') {
73
+ await update();
74
+ return;
75
+ }
76
+ const port = args.includes('--port') ? Number(args[args.indexOf('--port') + 1]) : Number(process.env.PORT ?? 3000);
77
+ const noOpen = args.includes('--no-open');
8
78
  console.log('');
9
79
  console.log(' UniLLM — Universal LLM Proxy');
10
80
  console.log(' One OpenAI-compatible endpoint for 15+ free providers');
@@ -76,6 +76,19 @@ export declare function getAnalytics(windowHours?: number): {
76
76
  tokens: number;
77
77
  }>;
78
78
  };
79
+ export type RequestLog = {
80
+ id: number;
81
+ ts: number;
82
+ platform: string;
83
+ model_id: string;
84
+ prompt_tokens: number;
85
+ complete_tokens: number;
86
+ latency_ms: number | null;
87
+ success: number;
88
+ error_code: string | null;
89
+ };
90
+ export declare function listRequests(limit?: number): RequestLog[];
91
+ export declare function clearAnalytics(): void;
79
92
  export declare function createSession(token: string, ttlSeconds?: number): void;
80
93
  export declare function validateSession(token: string): boolean;
81
94
  export declare function deleteSession(token: string): void;
package/dist/db/index.js CHANGED
@@ -28,6 +28,8 @@ exports.addCustomProvider = addCustomProvider;
28
28
  exports.deleteCustomProvider = deleteCustomProvider;
29
29
  exports.logRequest = logRequest;
30
30
  exports.getAnalytics = getAnalytics;
31
+ exports.listRequests = listRequests;
32
+ exports.clearAnalytics = clearAnalytics;
31
33
  exports.createSession = createSession;
32
34
  exports.validateSession = validateSession;
33
35
  exports.deleteSession = deleteSession;
@@ -216,8 +218,8 @@ function logRequest(data) {
216
218
  VALUES (?, ?, ?, ?, ?, ?, ?)
217
219
  `).run(data.platform, data.model_id, data.prompt_tokens, data.complete_tokens, data.latency_ms, data.success ? 1 : 0, data.error_code ?? null);
218
220
  }
219
- function getAnalytics(windowHours = 24) {
220
- const since = Math.floor(Date.now() / 1000) - windowHours * 3600;
221
+ function getAnalytics(windowHours) {
222
+ const since = windowHours ? Math.floor(Date.now() / 1000) - windowHours * 3600 : 0;
221
223
  const rows = db
222
224
  .prepare('SELECT * FROM analytics WHERE ts >= ?')
223
225
  .all(since);
@@ -246,6 +248,14 @@ function getAnalytics(windowHours = 24) {
246
248
  byPlatform,
247
249
  };
248
250
  }
251
+ function listRequests(limit = 5000) {
252
+ return db
253
+ .prepare('SELECT * FROM analytics ORDER BY id DESC LIMIT ?')
254
+ .all(limit);
255
+ }
256
+ function clearAnalytics() {
257
+ db.prepare('DELETE FROM analytics').run();
258
+ }
249
259
  // ─── Sessions ─────────────────────────────────────────────────────────────────
250
260
  function createSession(token, ttlSeconds = 86400 * 7) {
251
261
  const expires = Math.floor(Date.now() / 1000) + ttlSeconds;
@@ -175,6 +175,11 @@ class GoogleProvider extends types_1.BaseProvider {
175
175
  finish_reason: finishReason?.toLowerCase() ?? null,
176
176
  },
177
177
  ],
178
+ usage: data.usageMetadata ? {
179
+ prompt_tokens: data.usageMetadata.promptTokenCount ?? 0,
180
+ completion_tokens: data.usageMetadata.candidatesTokenCount ?? 0,
181
+ total_tokens: data.usageMetadata.totalTokenCount ?? 0,
182
+ } : undefined,
178
183
  });
179
184
  }
180
185
  catch { /* skip */ }
@@ -60,7 +60,7 @@ class OpenAICompatProvider extends types_1.BaseProvider {
60
60
  res = await fetch(`${this.baseUrl}/chat/completions`, {
61
61
  method: 'POST',
62
62
  headers,
63
- body: JSON.stringify({ ...req, stream: true }),
63
+ body: JSON.stringify({ ...req, stream: true, stream_options: { include_usage: true } }),
64
64
  });
65
65
  }
66
66
  catch (e) {
@@ -35,6 +35,9 @@ export type ChatRequest = {
35
35
  model: string;
36
36
  messages: ChatMessage[];
37
37
  stream?: boolean;
38
+ stream_options?: {
39
+ include_usage?: boolean;
40
+ };
38
41
  temperature?: number;
39
42
  max_tokens?: number;
40
43
  top_p?: number;
@@ -81,6 +84,11 @@ export type StreamChunk = {
81
84
  };
82
85
  finish_reason: string | null;
83
86
  }>;
87
+ usage?: {
88
+ prompt_tokens: number;
89
+ completion_tokens: number;
90
+ total_tokens: number;
91
+ };
84
92
  };
85
93
  export declare abstract class BaseProvider {
86
94
  abstract readonly platform: string;
@@ -5,6 +5,13 @@ const express_1 = require("express");
5
5
  const db_1 = require("../../db");
6
6
  exports.analyticsRouter = (0, express_1.Router)();
7
7
  exports.analyticsRouter.get('/analytics', (req, res) => {
8
- const hours = Number(req.query.hours) || 24;
8
+ const hours = Number(req.query.hours) || undefined;
9
9
  return res.json((0, db_1.getAnalytics)(hours));
10
10
  });
11
+ exports.analyticsRouter.get('/analytics/requests', (_req, res) => {
12
+ return res.json((0, db_1.listRequests)());
13
+ });
14
+ exports.analyticsRouter.delete('/analytics', (_req, res) => {
15
+ (0, db_1.clearAnalytics)();
16
+ return res.json({ ok: true });
17
+ });
@@ -50,10 +50,16 @@ exports.chatRouter.post('/chat/completions', async (req, res) => {
50
50
  let promptTokens = 0;
51
51
  let completionTokens = 0;
52
52
  let done = false;
53
- await provider.streamChat(candidate.decryptedKey, { ...body, model: platformModel }, (chunk) => {
54
- completionTokens += chunk.choices?.[0]?.delta?.content?.length
55
- ? Math.ceil(chunk.choices[0].delta.content.length / 4)
56
- : 0;
53
+ await provider.streamChat(candidate.decryptedKey, { ...body, model: platformModel, stream_options: { include_usage: true } }, (chunk) => {
54
+ if (chunk.usage) {
55
+ promptTokens = chunk.usage.prompt_tokens;
56
+ completionTokens = chunk.usage.completion_tokens;
57
+ }
58
+ else {
59
+ completionTokens += chunk.choices?.[0]?.delta?.content?.length
60
+ ? Math.ceil(chunk.choices[0].delta.content.length / 4)
61
+ : 0;
62
+ }
57
63
  res.write(`data: ${JSON.stringify(chunk)}\n\n`);
58
64
  }, () => {
59
65
  done = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "freellm",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "One OpenAI-compatible endpoint for 15+ free LLM providers. Smart routing, failover, encrypted keys. Run with npx freellm.",
5
5
  "keywords": [
6
6
  "llm",