freellm 1.0.13 → 1.0.14

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>
@@ -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.14",
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",