freellm 1.0.11 → 1.0.13
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/client-dist/assets/index-CZTExjJU.js +174 -0
- package/client-dist/index.html +1 -1
- package/dist/cli.js +3 -5
- package/dist/db/index.d.ts +13 -0
- package/dist/db/index.js +34 -0
- package/dist/index.js +7 -0
- package/dist/providers/google.js +6 -2
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.js +4 -0
- package/dist/providers/openai-compat.js +7 -3
- package/dist/providers/types.d.ts +1 -1
- package/dist/providers/types.js +3 -12
- package/dist/routes/api/auth.js +12 -6
- package/dist/routes/api/custom-providers.d.ts +1 -0
- package/dist/routes/api/custom-providers.js +48 -0
- package/dist/routes/api/keys.js +86 -6
- package/dist/services/health.js +1 -34
- package/dist/services/router.d.ts +2 -0
- package/dist/services/router.js +3 -6
- package/package.json +12 -3
- package/client-dist/assets/index-cDIZ2w-M.js +0 -96
package/client-dist/index.html
CHANGED
|
@@ -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-
|
|
11
|
+
<script type="module" crossorigin src="/assets/index-CZTExjJU.js"></script>
|
|
12
12
|
</head>
|
|
13
13
|
<body>
|
|
14
14
|
<div id="root"></div>
|
package/dist/cli.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
const index_1 = require("./index");
|
|
7
|
-
const open_1 = __importDefault(require("open"));
|
|
8
4
|
const args = process.argv.slice(2);
|
|
9
5
|
const port = args.includes('--port') ? Number(args[args.indexOf('--port') + 1]) : Number(process.env.PORT ?? 3000);
|
|
10
6
|
const noOpen = args.includes('--no-open');
|
|
@@ -27,7 +23,9 @@ async function main() {
|
|
|
27
23
|
console.log(' Press Ctrl+C to stop');
|
|
28
24
|
console.log('');
|
|
29
25
|
if (!noOpen) {
|
|
30
|
-
setTimeout(() =>
|
|
26
|
+
setTimeout(() => {
|
|
27
|
+
import('open').then(({ default: open }) => open(url)).catch(() => { });
|
|
28
|
+
}, 800);
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
31
|
catch (e) {
|
package/dist/db/index.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export type Model = {
|
|
|
23
23
|
context_length: number | null;
|
|
24
24
|
fallback_order: number;
|
|
25
25
|
enabled: number;
|
|
26
|
+
available: number;
|
|
26
27
|
};
|
|
27
28
|
export declare function getSetting(key: string): string | undefined;
|
|
28
29
|
export declare function setSetting(key: string, value: string): void;
|
|
@@ -38,11 +39,23 @@ export declare function listModels(type?: string): Model[];
|
|
|
38
39
|
export declare function listEnabledModels(type?: string): Model[];
|
|
39
40
|
export declare function updateModelOrder(id: number, order: number): void;
|
|
40
41
|
export declare function toggleModel(id: number, enabled: boolean): void;
|
|
42
|
+
export declare function setModelAvailability(id: number, available: number): void;
|
|
43
|
+
export declare function resetPlatformAvailability(platform: string): void;
|
|
41
44
|
export declare function listModelsByPlatform(platform: string): Model[];
|
|
42
45
|
export declare function upsertModelsFromList(platform: string, models: Array<{
|
|
43
46
|
id: string;
|
|
44
47
|
display_name?: string;
|
|
45
48
|
}>): number;
|
|
49
|
+
export type CustomProvider = {
|
|
50
|
+
platform: string;
|
|
51
|
+
name: string;
|
|
52
|
+
base_url: string;
|
|
53
|
+
default_model: string;
|
|
54
|
+
created_at: number;
|
|
55
|
+
};
|
|
56
|
+
export declare function listCustomProviders(): CustomProvider[];
|
|
57
|
+
export declare function addCustomProvider(platform: string, name: string, baseUrl: string, defaultModel: string): void;
|
|
58
|
+
export declare function deleteCustomProvider(platform: string): void;
|
|
46
59
|
export declare function logRequest(data: {
|
|
47
60
|
platform: string;
|
|
48
61
|
model_id: string;
|
package/dist/db/index.js
CHANGED
|
@@ -19,8 +19,13 @@ exports.listModels = listModels;
|
|
|
19
19
|
exports.listEnabledModels = listEnabledModels;
|
|
20
20
|
exports.updateModelOrder = updateModelOrder;
|
|
21
21
|
exports.toggleModel = toggleModel;
|
|
22
|
+
exports.setModelAvailability = setModelAvailability;
|
|
23
|
+
exports.resetPlatformAvailability = resetPlatformAvailability;
|
|
22
24
|
exports.listModelsByPlatform = listModelsByPlatform;
|
|
23
25
|
exports.upsertModelsFromList = upsertModelsFromList;
|
|
26
|
+
exports.listCustomProviders = listCustomProviders;
|
|
27
|
+
exports.addCustomProvider = addCustomProvider;
|
|
28
|
+
exports.deleteCustomProvider = deleteCustomProvider;
|
|
24
29
|
exports.logRequest = logRequest;
|
|
25
30
|
exports.getAnalytics = getAnalytics;
|
|
26
31
|
exports.createSession = createSession;
|
|
@@ -95,10 +100,22 @@ function migrate() {
|
|
|
95
100
|
error_code TEXT
|
|
96
101
|
);
|
|
97
102
|
|
|
103
|
+
CREATE TABLE IF NOT EXISTS custom_providers (
|
|
104
|
+
platform TEXT PRIMARY KEY,
|
|
105
|
+
name TEXT NOT NULL,
|
|
106
|
+
base_url TEXT NOT NULL,
|
|
107
|
+
default_model TEXT NOT NULL,
|
|
108
|
+
created_at INTEGER NOT NULL DEFAULT (unixepoch())
|
|
109
|
+
);
|
|
110
|
+
|
|
98
111
|
CREATE INDEX IF NOT EXISTS idx_analytics_ts ON analytics(ts);
|
|
99
112
|
CREATE INDEX IF NOT EXISTS idx_analytics_platform ON analytics(platform);
|
|
100
113
|
`);
|
|
101
114
|
seedModels();
|
|
115
|
+
const cols = db.prepare('PRAGMA table_info(models)').all();
|
|
116
|
+
if (!cols.some(c => c.name === 'available')) {
|
|
117
|
+
db.exec('ALTER TABLE models ADD COLUMN available INTEGER NOT NULL DEFAULT -1');
|
|
118
|
+
}
|
|
102
119
|
}
|
|
103
120
|
// ─── Settings ────────────────────────────────────────────────────────────────
|
|
104
121
|
function getSetting(key) {
|
|
@@ -154,6 +171,12 @@ function updateModelOrder(id, order) {
|
|
|
154
171
|
function toggleModel(id, enabled) {
|
|
155
172
|
db.prepare('UPDATE models SET enabled = ? WHERE id = ?').run(enabled ? 1 : 0, id);
|
|
156
173
|
}
|
|
174
|
+
function setModelAvailability(id, available) {
|
|
175
|
+
db.prepare('UPDATE models SET available = ? WHERE id = ?').run(available, id);
|
|
176
|
+
}
|
|
177
|
+
function resetPlatformAvailability(platform) {
|
|
178
|
+
db.prepare('UPDATE models SET available = -1 WHERE platform = ?').run(platform);
|
|
179
|
+
}
|
|
157
180
|
function listModelsByPlatform(platform) {
|
|
158
181
|
return db
|
|
159
182
|
.prepare('SELECT * FROM models WHERE platform = ? ORDER BY fallback_order, id')
|
|
@@ -175,6 +198,17 @@ function upsertModelsFromList(platform, models) {
|
|
|
175
198
|
}
|
|
176
199
|
return added;
|
|
177
200
|
}
|
|
201
|
+
function listCustomProviders() {
|
|
202
|
+
return db.prepare('SELECT * FROM custom_providers ORDER BY created_at').all();
|
|
203
|
+
}
|
|
204
|
+
function addCustomProvider(platform, name, baseUrl, defaultModel) {
|
|
205
|
+
db.prepare('INSERT INTO custom_providers (platform, name, base_url, default_model) VALUES (?, ?, ?, ?)').run(platform, name, baseUrl, defaultModel);
|
|
206
|
+
}
|
|
207
|
+
function deleteCustomProvider(platform) {
|
|
208
|
+
db.prepare('DELETE FROM custom_providers WHERE platform = ?').run(platform);
|
|
209
|
+
db.prepare('DELETE FROM provider_keys WHERE platform = ?').run(platform);
|
|
210
|
+
db.prepare('DELETE FROM models WHERE platform = ?').run(platform);
|
|
211
|
+
}
|
|
178
212
|
// ─── Analytics ───────────────────────────────────────────────────────────────
|
|
179
213
|
function logRequest(data) {
|
|
180
214
|
db.prepare(`
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,7 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
13
13
|
const crypto_1 = __importDefault(require("crypto"));
|
|
14
14
|
const crypto_2 = require("./crypto");
|
|
15
15
|
const index_1 = require("./db/index");
|
|
16
|
+
const providers_1 = require("./providers");
|
|
16
17
|
const chat_1 = require("./routes/v1/chat");
|
|
17
18
|
const models_1 = require("./routes/v1/models");
|
|
18
19
|
const images_1 = require("./routes/v1/images");
|
|
@@ -21,6 +22,7 @@ const auth_1 = require("./routes/api/auth");
|
|
|
21
22
|
const keys_1 = require("./routes/api/keys");
|
|
22
23
|
const models_2 = require("./routes/api/models");
|
|
23
24
|
const analytics_1 = require("./routes/api/analytics");
|
|
25
|
+
const custom_providers_1 = require("./routes/api/custom-providers");
|
|
24
26
|
const health_1 = require("./services/health");
|
|
25
27
|
async function startServer(config = {}) {
|
|
26
28
|
const port = config.port ?? Number(process.env.PORT ?? 3000);
|
|
@@ -42,6 +44,10 @@ async function startServer(config = {}) {
|
|
|
42
44
|
}
|
|
43
45
|
(0, crypto_2.initCrypto)(encKey);
|
|
44
46
|
(0, index_1.initDb)(dbPath);
|
|
47
|
+
// Register persisted custom providers
|
|
48
|
+
for (const p of (0, index_1.listCustomProviders)()) {
|
|
49
|
+
(0, providers_1.registerCustomProvider)(p.platform, p.base_url, p.default_model);
|
|
50
|
+
}
|
|
45
51
|
// Ensure unified key exists
|
|
46
52
|
if (!(0, index_1.getSetting)('unified_key')) {
|
|
47
53
|
(0, index_1.setSetting)('unified_key', (0, crypto_2.generateUnifiedKey)());
|
|
@@ -74,6 +80,7 @@ async function startServer(config = {}) {
|
|
|
74
80
|
app.use('/api', auth_1.requireAuth, keys_1.keysRouter);
|
|
75
81
|
app.use('/api', auth_1.requireAuth, models_2.modelsApiRouter);
|
|
76
82
|
app.use('/api', auth_1.requireAuth, analytics_1.analyticsRouter);
|
|
83
|
+
app.use('/api', auth_1.requireAuth, custom_providers_1.customProvidersRouter);
|
|
77
84
|
// Serve React dashboard
|
|
78
85
|
const clientDist = path_1.default.join(__dirname, '..', 'client-dist');
|
|
79
86
|
if (fs_1.default.existsSync(clientDist)) {
|
package/dist/providers/google.js
CHANGED
|
@@ -47,8 +47,12 @@ class GoogleProvider extends types_1.BaseProvider {
|
|
|
47
47
|
}
|
|
48
48
|
async listModels(apiKey) {
|
|
49
49
|
const res = await fetch(`${this.baseUrl}/models?key=${encodeURIComponent(apiKey)}`);
|
|
50
|
-
if (!res.ok)
|
|
51
|
-
|
|
50
|
+
if (!res.ok) {
|
|
51
|
+
const text = await res.text().catch(() => '');
|
|
52
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
53
|
+
err.status = res.status;
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
52
56
|
const data = await res.json();
|
|
53
57
|
return (data.models ?? [])
|
|
54
58
|
.filter((m) => !!m.name && (m.supportedGenerationMethods ?? []).includes('generateContent'))
|
|
@@ -12,3 +12,4 @@ export declare function getProvider(platform: string, type: 'image'): BaseImageP
|
|
|
12
12
|
export declare function getProvider(platform: string, type: 'tts'): BaseTTSProvider | undefined;
|
|
13
13
|
export declare function allProviders(type?: 'chat' | 'image' | 'tts'): string[];
|
|
14
14
|
export declare function registerCustomProvider(platform: string, baseUrl: string, defaultModel: string): void;
|
|
15
|
+
export declare function unregisterCustomProvider(platform: string): void;
|
package/dist/providers/index.js
CHANGED
|
@@ -7,6 +7,7 @@ exports.getTTSProvider = getTTSProvider;
|
|
|
7
7
|
exports.getProvider = getProvider;
|
|
8
8
|
exports.allProviders = allProviders;
|
|
9
9
|
exports.registerCustomProvider = registerCustomProvider;
|
|
10
|
+
exports.unregisterCustomProvider = unregisterCustomProvider;
|
|
10
11
|
const openai_compat_1 = require("./openai-compat");
|
|
11
12
|
Object.defineProperty(exports, "OpenAICompatProvider", { enumerable: true, get: function () { return openai_compat_1.OpenAICompatProvider; } });
|
|
12
13
|
const google_1 = require("./google");
|
|
@@ -58,3 +59,6 @@ function allProviders(type = 'chat') {
|
|
|
58
59
|
function registerCustomProvider(platform, baseUrl, defaultModel) {
|
|
59
60
|
chatRegistry[platform] = new openai_compat_1.OpenAICompatProvider(platform, baseUrl, defaultModel);
|
|
60
61
|
}
|
|
62
|
+
function unregisterCustomProvider(platform) {
|
|
63
|
+
delete chatRegistry[platform];
|
|
64
|
+
}
|
|
@@ -18,8 +18,12 @@ class OpenAICompatProvider extends types_1.BaseProvider {
|
|
|
18
18
|
if (apiKey)
|
|
19
19
|
headers['Authorization'] = `Bearer ${apiKey}`;
|
|
20
20
|
const res = await fetch(`${this.baseUrl}/models`, { headers });
|
|
21
|
-
if (!res.ok)
|
|
22
|
-
|
|
21
|
+
if (!res.ok) {
|
|
22
|
+
const text = await res.text().catch(() => '');
|
|
23
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
24
|
+
err.status = res.status;
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
23
27
|
const data = await res.json();
|
|
24
28
|
return (data.data ?? [])
|
|
25
29
|
.filter((m) => !!m.id)
|
|
@@ -120,7 +124,7 @@ exports.PROVIDERS = {
|
|
|
120
124
|
cohere: new OpenAICompatProvider('cohere', 'https://api.cohere.com/compatibility/v1', 'command-r'),
|
|
121
125
|
llm7: new OpenAICompatProvider('llm7', 'https://llm7.io/v1', 'gpt-4o-mini'),
|
|
122
126
|
ovh: new OpenAICompatProvider('ovh', 'https://oai.endpoints.kepler.ai.cloud.ovh.net/v1', 'Llama-3.3-70B-Instruct'),
|
|
123
|
-
nvidia: new OpenAICompatProvider('nvidia', 'https://integrate.api.nvidia.com/v1', 'deepseek-ai/deepseek-
|
|
127
|
+
nvidia: new OpenAICompatProvider('nvidia', 'https://integrate.api.nvidia.com/v1', 'deepseek-ai/deepseek-coder-6.7b-instruct'),
|
|
124
128
|
kilo: new OpenAICompatProvider('kilo', 'https://gateway.kilo.chat/v1', 'poolside/laguna-m.1:free'),
|
|
125
129
|
opencode: new OpenAICompatProvider('opencode', 'https://api.opencode.ai/v1', 'deepseek-v4-flash-free'),
|
|
126
130
|
zhipu: new OpenAICompatProvider('zhipu', 'https://open.bigmodel.cn/api/paas/v4', 'glm-4.7-flash'),
|
|
@@ -88,7 +88,7 @@ export declare abstract class BaseProvider {
|
|
|
88
88
|
protected fetchJSON<T>(url: string, options: RequestInit): Promise<T>;
|
|
89
89
|
abstract chat(apiKey: string, req: ChatRequest): Promise<ChatResponse>;
|
|
90
90
|
abstract streamChat(apiKey: string, req: ChatRequest, onChunk: (chunk: StreamChunk) => void, onDone: () => void, onError: (err: Error) => void): Promise<void>;
|
|
91
|
-
/** Default implementation:
|
|
91
|
+
/** Default implementation: verify the key via the provider's /models endpoint; throws on failure */
|
|
92
92
|
healthCheck(apiKey: string): Promise<boolean>;
|
|
93
93
|
/** Fetch available models from the provider. Default: none (provider does not expose a list). */
|
|
94
94
|
listModels(_apiKey: string): Promise<Array<{
|
package/dist/providers/types.js
CHANGED
|
@@ -13,19 +13,10 @@ class BaseProvider {
|
|
|
13
13
|
}
|
|
14
14
|
return res.json();
|
|
15
15
|
}
|
|
16
|
-
/** Default implementation:
|
|
16
|
+
/** Default implementation: verify the key via the provider's /models endpoint; throws on failure */
|
|
17
17
|
async healthCheck(apiKey) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
model: this.defaultModel(),
|
|
21
|
-
messages: [{ role: 'user', content: 'hi' }],
|
|
22
|
-
max_tokens: 1,
|
|
23
|
-
});
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
catch {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
18
|
+
await this.listModels(apiKey);
|
|
19
|
+
return true;
|
|
29
20
|
}
|
|
30
21
|
/** Fetch available models from the provider. Default: none (provider does not expose a list). */
|
|
31
22
|
async listModels(_apiKey) {
|
package/dist/routes/api/auth.js
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
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
|
exports.authRouter = void 0;
|
|
4
7
|
exports.requireAuth = requireAuth;
|
|
5
8
|
const express_1 = require("express");
|
|
6
|
-
const
|
|
9
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
7
10
|
const db_1 = require("../../db");
|
|
8
|
-
const
|
|
11
|
+
const crypto_2 = require("../../crypto");
|
|
12
|
+
function makeToken() {
|
|
13
|
+
return crypto_1.default.randomBytes(36).toString('base64url');
|
|
14
|
+
}
|
|
9
15
|
exports.authRouter = (0, express_1.Router)();
|
|
10
16
|
// Middleware: protect API routes with session token
|
|
11
17
|
function requireAuth(req, res, next) {
|
|
@@ -32,8 +38,8 @@ exports.authRouter.post('/auth/setup', (req, res) => {
|
|
|
32
38
|
if ((0, db_1.getSetting)('password_hash')) {
|
|
33
39
|
return res.status(409).json({ error: 'Already set up' });
|
|
34
40
|
}
|
|
35
|
-
(0, db_1.setSetting)('password_hash', (0,
|
|
36
|
-
const token = (
|
|
41
|
+
(0, db_1.setSetting)('password_hash', (0, crypto_2.hashPassword)(password));
|
|
42
|
+
const token = makeToken();
|
|
37
43
|
(0, db_1.createSession)(token);
|
|
38
44
|
res.cookie('freellm-session', token, { httpOnly: true, sameSite: 'strict' });
|
|
39
45
|
return res.json({ token });
|
|
@@ -42,10 +48,10 @@ exports.authRouter.post('/auth/setup', (req, res) => {
|
|
|
42
48
|
exports.authRouter.post('/auth/login', (req, res) => {
|
|
43
49
|
const { password } = req.body;
|
|
44
50
|
const hash = (0, db_1.getSetting)('password_hash');
|
|
45
|
-
if (!hash || !password || !(0,
|
|
51
|
+
if (!hash || !password || !(0, crypto_2.verifyPassword)(password, hash)) {
|
|
46
52
|
return res.status(401).json({ error: 'Invalid password' });
|
|
47
53
|
}
|
|
48
|
-
const token = (
|
|
54
|
+
const token = makeToken();
|
|
49
55
|
(0, db_1.createSession)(token);
|
|
50
56
|
res.cookie('freellm-session', token, { httpOnly: true, sameSite: 'strict' });
|
|
51
57
|
return res.json({ token });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const customProvidersRouter: import("express-serve-static-core").Router;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.customProvidersRouter = void 0;
|
|
4
|
+
const express_1 = require("express");
|
|
5
|
+
const db_1 = require("../../db");
|
|
6
|
+
const providers_1 = require("../../providers");
|
|
7
|
+
exports.customProvidersRouter = (0, express_1.Router)();
|
|
8
|
+
function slugify(name) {
|
|
9
|
+
return name
|
|
10
|
+
.toLowerCase()
|
|
11
|
+
.trim()
|
|
12
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
13
|
+
.replace(/^-+|-+$/g, '');
|
|
14
|
+
}
|
|
15
|
+
exports.customProvidersRouter.get('/custom-providers', (_req, res) => {
|
|
16
|
+
return res.json((0, db_1.listCustomProviders)().map((p) => ({
|
|
17
|
+
platform: p.platform,
|
|
18
|
+
name: p.name,
|
|
19
|
+
baseUrl: p.base_url,
|
|
20
|
+
defaultModel: p.default_model,
|
|
21
|
+
})));
|
|
22
|
+
});
|
|
23
|
+
exports.customProvidersRouter.post('/custom-providers', (req, res) => {
|
|
24
|
+
const { name, baseUrl, defaultModel, apiKey } = req.body;
|
|
25
|
+
if (!name || !baseUrl || !defaultModel) {
|
|
26
|
+
return res.status(400).json({ error: 'name, baseUrl and defaultModel are required' });
|
|
27
|
+
}
|
|
28
|
+
const platform = slugify(name);
|
|
29
|
+
if (!platform) {
|
|
30
|
+
return res.status(400).json({ error: 'provider name must contain letters or numbers' });
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
(0, db_1.addCustomProvider)(platform, name.trim(), baseUrl.trim(), defaultModel.trim());
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return res.status(409).json({ error: `provider '${name}' already exists` });
|
|
37
|
+
}
|
|
38
|
+
(0, providers_1.registerCustomProvider)(platform, baseUrl.trim(), defaultModel.trim());
|
|
39
|
+
// Always create a key row (empty key for providers without auth) so the
|
|
40
|
+
// platform can sync models and appear in /v1/models
|
|
41
|
+
const keyId = (0, db_1.addKey)(platform, (apiKey ?? '').trim(), name.trim());
|
|
42
|
+
return res.status(201).json({ platform, keyId });
|
|
43
|
+
});
|
|
44
|
+
exports.customProvidersRouter.delete('/custom-providers/:platform', (req, res) => {
|
|
45
|
+
(0, db_1.deleteCustomProvider)(req.params.platform);
|
|
46
|
+
(0, providers_1.unregisterCustomProvider)(req.params.platform);
|
|
47
|
+
return res.json({ ok: true });
|
|
48
|
+
});
|
package/dist/routes/api/keys.js
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
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
|
exports.keysRouter = void 0;
|
|
4
7
|
const express_1 = require("express");
|
|
8
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
5
9
|
const db_1 = require("../../db");
|
|
6
10
|
const db_2 = require("../../db");
|
|
7
|
-
const
|
|
11
|
+
const crypto_2 = require("../../crypto");
|
|
8
12
|
const providers_1 = require("../../providers");
|
|
9
13
|
exports.keysRouter = (0, express_1.Router)();
|
|
10
14
|
// Get unified API key (or generate one)
|
|
11
15
|
exports.keysRouter.get('/keys/unified', (_req, res) => {
|
|
12
16
|
let key = (0, db_2.getSetting)('unified_key');
|
|
13
17
|
if (!key) {
|
|
14
|
-
key = (0,
|
|
18
|
+
key = (0, crypto_2.generateUnifiedKey)();
|
|
15
19
|
(0, db_2.setSetting)('unified_key', key);
|
|
16
20
|
}
|
|
17
21
|
return res.json({ key });
|
|
18
22
|
});
|
|
19
23
|
// Regenerate unified key
|
|
20
24
|
exports.keysRouter.post('/keys/unified/regenerate', (_req, res) => {
|
|
21
|
-
const key = (0,
|
|
25
|
+
const key = (0, crypto_2.generateUnifiedKey)();
|
|
22
26
|
(0, db_2.setSetting)('unified_key', key);
|
|
23
27
|
return res.json({ key });
|
|
24
28
|
});
|
|
@@ -47,10 +51,13 @@ exports.keysRouter.get('/keys', (_req, res) => {
|
|
|
47
51
|
// Add provider key
|
|
48
52
|
exports.keysRouter.post('/keys', (req, res) => {
|
|
49
53
|
const { platform, key, label } = req.body;
|
|
50
|
-
if (!platform
|
|
51
|
-
return res.status(400).json({ error: 'platform
|
|
54
|
+
if (!platform) {
|
|
55
|
+
return res.status(400).json({ error: 'platform is required' });
|
|
52
56
|
}
|
|
53
|
-
|
|
57
|
+
if (!key && !KEYLESS_PLATFORMS.has(platform)) {
|
|
58
|
+
return res.status(400).json({ error: 'key is required for this platform' });
|
|
59
|
+
}
|
|
60
|
+
const id = (0, db_1.addKey)(platform, key ?? '', label);
|
|
54
61
|
return res.json({ id });
|
|
55
62
|
});
|
|
56
63
|
// Delete provider key
|
|
@@ -116,3 +123,76 @@ exports.keysRouter.post('/keys/:id/models/sync', async (req, res) => {
|
|
|
116
123
|
return res.status(500).json({ error: e.message });
|
|
117
124
|
}
|
|
118
125
|
});
|
|
126
|
+
const checkJobs = new Map();
|
|
127
|
+
const CHECK_PACE_MS = 1500;
|
|
128
|
+
const KEYLESS_PLATFORMS = new Set(['pollinations', 'llm7', 'ovh', 'ollama', 'kilo']);
|
|
129
|
+
// Start checking every chat model of this key against the provider, one by one
|
|
130
|
+
exports.keysRouter.post('/keys/:id/models/check', async (req, res) => {
|
|
131
|
+
const k = (0, db_1.getKey)(Number(req.params.id));
|
|
132
|
+
if (!k)
|
|
133
|
+
return res.status(404).json({ error: 'key not found' });
|
|
134
|
+
const provider = (0, providers_1.getChatProvider)(k.platform);
|
|
135
|
+
if (!provider)
|
|
136
|
+
return res.status(400).json({ error: `no provider for platform '${k.platform}'` });
|
|
137
|
+
for (const [id, job] of checkJobs) {
|
|
138
|
+
if (job.keyId === k.id && !job.done)
|
|
139
|
+
return res.json({ jobId: id, total: job.total });
|
|
140
|
+
}
|
|
141
|
+
const models = (0, db_1.listModelsByPlatform)(k.platform).filter((m) => m.type === 'chat');
|
|
142
|
+
const job = {
|
|
143
|
+
keyId: k.id,
|
|
144
|
+
total: models.length,
|
|
145
|
+
checked: 0,
|
|
146
|
+
current: '',
|
|
147
|
+
available: {},
|
|
148
|
+
done: false,
|
|
149
|
+
};
|
|
150
|
+
const jobId = crypto_1.default.randomBytes(8).toString('hex');
|
|
151
|
+
checkJobs.set(jobId, job);
|
|
152
|
+
const rawKey = (0, db_1.decryptKey)(k.key_enc);
|
|
153
|
+
(0, db_1.resetPlatformAvailability)(k.platform);
|
|
154
|
+
(async () => {
|
|
155
|
+
try {
|
|
156
|
+
for (const m of models) {
|
|
157
|
+
job.current = m.model_id;
|
|
158
|
+
try {
|
|
159
|
+
await Promise.race([
|
|
160
|
+
provider.chat(rawKey, {
|
|
161
|
+
model: m.model_id,
|
|
162
|
+
messages: [{ role: 'user', content: 'hi' }],
|
|
163
|
+
max_tokens: 1,
|
|
164
|
+
}),
|
|
165
|
+
new Promise((_resolve, reject) => setTimeout(() => reject(new Error('timeout')), 30000)),
|
|
166
|
+
]);
|
|
167
|
+
job.available[m.id] = 1;
|
|
168
|
+
(0, db_1.setModelAvailability)(m.id, 1);
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
job.available[m.id] = 0;
|
|
172
|
+
(0, db_1.setModelAvailability)(m.id, 0);
|
|
173
|
+
}
|
|
174
|
+
job.checked++;
|
|
175
|
+
await new Promise((r) => setTimeout(r, CHECK_PACE_MS));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
job.done = true;
|
|
180
|
+
job.current = '';
|
|
181
|
+
setTimeout(() => checkJobs.delete(jobId), 10 * 60 * 1000);
|
|
182
|
+
}
|
|
183
|
+
})();
|
|
184
|
+
return res.json({ jobId, total: models.length });
|
|
185
|
+
});
|
|
186
|
+
// Poll check progress
|
|
187
|
+
exports.keysRouter.get('/keys/:id/models/check/:jobId', (_req, res) => {
|
|
188
|
+
const job = checkJobs.get(_req.params.jobId);
|
|
189
|
+
if (!job)
|
|
190
|
+
return res.status(404).json({ error: 'job not found' });
|
|
191
|
+
return res.json({
|
|
192
|
+
done: job.done,
|
|
193
|
+
total: job.total,
|
|
194
|
+
checked: job.checked,
|
|
195
|
+
current: job.current,
|
|
196
|
+
available: job.available,
|
|
197
|
+
});
|
|
198
|
+
});
|
package/dist/services/health.js
CHANGED
|
@@ -1,37 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
3
|
exports.startHealthService = startHealthService;
|
|
37
4
|
exports.stopHealthService = stopHealthService;
|
|
@@ -59,7 +26,7 @@ async function runHealthChecks() {
|
|
|
59
26
|
if (!provider)
|
|
60
27
|
continue;
|
|
61
28
|
try {
|
|
62
|
-
const { decrypt } = await
|
|
29
|
+
const { decrypt } = await import('../crypto.js');
|
|
63
30
|
const rawKey = decrypt(k.key_enc);
|
|
64
31
|
const ok = await provider.healthCheck(rawKey);
|
|
65
32
|
(0, db_1.updateKeyStatus)(k.id, ok ? 'healthy' : 'error');
|
|
@@ -6,6 +6,8 @@ export type RouterEntry = {
|
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
8
8
|
* Returns an ordered list of (model, key) candidates for a request.
|
|
9
|
+
* Follows the model priority list (fallback order) and only includes
|
|
10
|
+
* platforms that have an added, enabled, healthy key.
|
|
9
11
|
* Filters out disabled models, rate-limited/cooldown keys, and unhealthy keys.
|
|
10
12
|
*/
|
|
11
13
|
export declare function buildCandidates(requestedModel: string, type?: string, requireVision?: boolean, requireTools?: boolean): RouterEntry[];
|
package/dist/services/router.js
CHANGED
|
@@ -5,9 +5,10 @@ exports.hasVisionContent = hasVisionContent;
|
|
|
5
5
|
const db_1 = require("../db");
|
|
6
6
|
const providers_1 = require("../providers");
|
|
7
7
|
const ratelimit_1 = require("./ratelimit");
|
|
8
|
-
const KEYLESS_PLATFORMS = new Set(['pollinations', 'llm7', 'ovh', 'ollama', 'kilo']);
|
|
9
8
|
/**
|
|
10
9
|
* Returns an ordered list of (model, key) candidates for a request.
|
|
10
|
+
* Follows the model priority list (fallback order) and only includes
|
|
11
|
+
* platforms that have an added, enabled, healthy key.
|
|
11
12
|
* Filters out disabled models, rate-limited/cooldown keys, and unhealthy keys.
|
|
12
13
|
*/
|
|
13
14
|
function buildCandidates(requestedModel, type = 'chat', requireVision = false, requireTools = false) {
|
|
@@ -42,11 +43,7 @@ function buildCandidates(requestedModel, type = 'chat', requireVision = false, r
|
|
|
42
43
|
provider = (0, providers_1.getTTSProvider)(model.platform);
|
|
43
44
|
if (!provider)
|
|
44
45
|
continue;
|
|
45
|
-
//
|
|
46
|
-
if (KEYLESS_PLATFORMS.has(model.platform)) {
|
|
47
|
-
candidates.push({ model, providerKey: null, decryptedKey: '' });
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
46
|
+
// Providers are only used when a key has been added for them
|
|
50
47
|
const platformKeys = keysByPlatform[model.platform] ?? [];
|
|
51
48
|
for (const pk of platformKeys) {
|
|
52
49
|
if ((0, ratelimit_1.isOnCooldown)(model.platform, pk.id))
|
package/package.json
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "freellm",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.13",
|
|
4
4
|
"description": "One OpenAI-compatible endpoint for 15+ free LLM providers. Smart routing, failover, encrypted keys. Run with npx freellm.",
|
|
5
|
-
"keywords": [
|
|
5
|
+
"keywords": [
|
|
6
|
+
"llm",
|
|
7
|
+
"openai",
|
|
8
|
+
"proxy",
|
|
9
|
+
"groq",
|
|
10
|
+
"gemini",
|
|
11
|
+
"mistral",
|
|
12
|
+
"free",
|
|
13
|
+
"api-gateway"
|
|
14
|
+
],
|
|
6
15
|
"author": "",
|
|
7
16
|
"license": "MIT",
|
|
8
17
|
"bin": {
|
|
@@ -31,7 +40,7 @@
|
|
|
31
40
|
"cookie-parser": "^1.4.6",
|
|
32
41
|
"cors": "^2.8.5",
|
|
33
42
|
"express": "^4.18.3",
|
|
34
|
-
"
|
|
43
|
+
"marked": "^18.0.9",
|
|
35
44
|
"open": "^10.1.0",
|
|
36
45
|
"react": "^18.2.0",
|
|
37
46
|
"react-dom": "^18.2.0"
|