freellm 1.0.15 → 1.0.16
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/bin/cli.js +1 -1
- package/client-dist/assets/index-Df7FYw88.js +96 -0
- package/client-dist/index.html +1 -1
- package/dist/cli.js +1 -1
- package/dist/db/index.d.ts +7 -17
- package/dist/db/index.js +38 -24
- package/dist/index.js +1 -1
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +1 -1
- package/dist/providers/openai-compat.d.ts +1 -1
- package/dist/providers/openai-compat.js +1 -1
- package/dist/providers/types.d.ts +5 -1
- package/dist/providers/types.js +28 -2
- package/dist/routes/api/analytics.js +2 -6
- package/dist/routes/api/custom-providers.js +37 -4
- package/dist/routes/api/keys.js +26 -78
- package/dist/routes/api/models.js +1 -1
- package/package.json +1 -1
- package/client-dist/assets/index-DyoXu6aO.js +0 -174
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-Df7FYw88.js"></script>
|
|
12
12
|
</head>
|
|
13
13
|
<body>
|
|
14
14
|
<div id="root"></div>
|
package/dist/cli.js
CHANGED
|
@@ -76,7 +76,7 @@ async function main() {
|
|
|
76
76
|
const port = args.includes('--port') ? Number(args[args.indexOf('--port') + 1]) : Number(process.env.PORT ?? 3000);
|
|
77
77
|
const noOpen = args.includes('--no-open');
|
|
78
78
|
console.log('');
|
|
79
|
-
console.log('
|
|
79
|
+
console.log(' FreeLLM — Universal LLM Proxy');
|
|
80
80
|
console.log(' One OpenAI-compatible endpoint for 15+ free providers');
|
|
81
81
|
console.log('');
|
|
82
82
|
try {
|
package/dist/db/index.d.ts
CHANGED
|
@@ -23,7 +23,6 @@ export type Model = {
|
|
|
23
23
|
context_length: number | null;
|
|
24
24
|
fallback_order: number;
|
|
25
25
|
enabled: number;
|
|
26
|
-
available: number;
|
|
27
26
|
};
|
|
28
27
|
export declare function getSetting(key: string): string | undefined;
|
|
29
28
|
export declare function setSetting(key: string, value: string): void;
|
|
@@ -39,9 +38,11 @@ export declare function listModels(type?: string): Model[];
|
|
|
39
38
|
export declare function listEnabledModels(type?: string): Model[];
|
|
40
39
|
export declare function updateModelOrder(id: number, order: number): void;
|
|
41
40
|
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;
|
|
44
41
|
export declare function listModelsByPlatform(platform: string): Model[];
|
|
42
|
+
export declare function updateModelsAvailability(platform: string, modelIds: string[]): {
|
|
43
|
+
enabled: string[];
|
|
44
|
+
disabled: string[];
|
|
45
|
+
};
|
|
45
46
|
export declare function upsertModelsFromList(platform: string, models: Array<{
|
|
46
47
|
id: string;
|
|
47
48
|
display_name?: string;
|
|
@@ -54,7 +55,8 @@ export type CustomProvider = {
|
|
|
54
55
|
created_at: number;
|
|
55
56
|
};
|
|
56
57
|
export declare function listCustomProviders(): CustomProvider[];
|
|
57
|
-
export declare function
|
|
58
|
+
export declare function getCustomProvider(platform: string): CustomProvider | undefined;
|
|
59
|
+
export declare function addCustomProvider(platform: string, name: string, baseUrl: string, defaultModel?: string): void;
|
|
58
60
|
export declare function deleteCustomProvider(platform: string): void;
|
|
59
61
|
export declare function logRequest(data: {
|
|
60
62
|
platform: string;
|
|
@@ -65,6 +67,7 @@ export declare function logRequest(data: {
|
|
|
65
67
|
success: boolean;
|
|
66
68
|
error_code?: string;
|
|
67
69
|
}): void;
|
|
70
|
+
export declare function clearAnalytics(): number;
|
|
68
71
|
export declare function getAnalytics(windowHours?: number): {
|
|
69
72
|
total: number;
|
|
70
73
|
success: number;
|
|
@@ -76,19 +79,6 @@ export declare function getAnalytics(windowHours?: number): {
|
|
|
76
79
|
tokens: number;
|
|
77
80
|
}>;
|
|
78
81
|
};
|
|
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;
|
|
92
82
|
export declare function createSession(token: string, ttlSeconds?: number): void;
|
|
93
83
|
export declare function validateSession(token: string): boolean;
|
|
94
84
|
export declare function deleteSession(token: string): void;
|
package/dist/db/index.js
CHANGED
|
@@ -19,17 +19,16 @@ 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;
|
|
24
22
|
exports.listModelsByPlatform = listModelsByPlatform;
|
|
23
|
+
exports.updateModelsAvailability = updateModelsAvailability;
|
|
25
24
|
exports.upsertModelsFromList = upsertModelsFromList;
|
|
26
25
|
exports.listCustomProviders = listCustomProviders;
|
|
26
|
+
exports.getCustomProvider = getCustomProvider;
|
|
27
27
|
exports.addCustomProvider = addCustomProvider;
|
|
28
28
|
exports.deleteCustomProvider = deleteCustomProvider;
|
|
29
29
|
exports.logRequest = logRequest;
|
|
30
|
-
exports.getAnalytics = getAnalytics;
|
|
31
|
-
exports.listRequests = listRequests;
|
|
32
30
|
exports.clearAnalytics = clearAnalytics;
|
|
31
|
+
exports.getAnalytics = getAnalytics;
|
|
33
32
|
exports.createSession = createSession;
|
|
34
33
|
exports.validateSession = validateSession;
|
|
35
34
|
exports.deleteSession = deleteSession;
|
|
@@ -114,9 +113,12 @@ function migrate() {
|
|
|
114
113
|
CREATE INDEX IF NOT EXISTS idx_analytics_platform ON analytics(platform);
|
|
115
114
|
`);
|
|
116
115
|
seedModels();
|
|
117
|
-
|
|
118
|
-
if
|
|
119
|
-
|
|
116
|
+
// Make sure providers added through the dashboard always have at least
|
|
117
|
+
// their default model available, even if they do not expose /models.
|
|
118
|
+
for (const provider of listCustomProviders()) {
|
|
119
|
+
if (provider.default_model) {
|
|
120
|
+
upsertModelsFromList(provider.platform, [{ id: provider.default_model }]);
|
|
121
|
+
}
|
|
120
122
|
}
|
|
121
123
|
}
|
|
122
124
|
// ─── Settings ────────────────────────────────────────────────────────────────
|
|
@@ -173,17 +175,24 @@ function updateModelOrder(id, order) {
|
|
|
173
175
|
function toggleModel(id, enabled) {
|
|
174
176
|
db.prepare('UPDATE models SET enabled = ? WHERE id = ?').run(enabled ? 1 : 0, id);
|
|
175
177
|
}
|
|
176
|
-
function setModelAvailability(id, available) {
|
|
177
|
-
db.prepare('UPDATE models SET available = ? WHERE id = ?').run(available, id);
|
|
178
|
-
}
|
|
179
|
-
function resetPlatformAvailability(platform) {
|
|
180
|
-
db.prepare('UPDATE models SET available = -1 WHERE platform = ?').run(platform);
|
|
181
|
-
}
|
|
182
178
|
function listModelsByPlatform(platform) {
|
|
183
179
|
return db
|
|
184
180
|
.prepare('SELECT * FROM models WHERE platform = ? ORDER BY fallback_order, id')
|
|
185
181
|
.all(platform);
|
|
186
182
|
}
|
|
183
|
+
function updateModelsAvailability(platform, modelIds) {
|
|
184
|
+
if (modelIds.length === 0)
|
|
185
|
+
return { enabled: [], disabled: [] };
|
|
186
|
+
const placeholders = modelIds.map(() => '?').join(', ');
|
|
187
|
+
const unavailable = db.prepare(`SELECT model_id FROM models WHERE platform = ? AND type = 'chat' AND model_id NOT IN (${placeholders})`).all(platform, ...modelIds);
|
|
188
|
+
const unavailableIds = unavailable.map(m => m.model_id);
|
|
189
|
+
const enabled = db.prepare(`SELECT model_id FROM models WHERE platform = ? AND type = 'chat' AND model_id IN (${placeholders}) AND enabled = 0`).all(platform, ...modelIds);
|
|
190
|
+
db.prepare(`UPDATE models SET enabled = CASE WHEN model_id IN (${placeholders}) THEN 1 ELSE 0 END WHERE platform = ? AND type = 'chat'`).run(...modelIds, platform);
|
|
191
|
+
return {
|
|
192
|
+
enabled: enabled.map(m => m.model_id),
|
|
193
|
+
disabled: unavailableIds,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
187
196
|
function upsertModelsFromList(platform, models) {
|
|
188
197
|
const stmt = db.prepare(`
|
|
189
198
|
INSERT INTO models (platform, model_id, display_name, supports_tools, supports_vision, supports_stream, type, context_length, fallback_order, enabled)
|
|
@@ -203,8 +212,17 @@ function upsertModelsFromList(platform, models) {
|
|
|
203
212
|
function listCustomProviders() {
|
|
204
213
|
return db.prepare('SELECT * FROM custom_providers ORDER BY created_at').all();
|
|
205
214
|
}
|
|
206
|
-
function
|
|
215
|
+
function getCustomProvider(platform) {
|
|
216
|
+
return db.prepare('SELECT * FROM custom_providers WHERE platform = ?').get(platform);
|
|
217
|
+
}
|
|
218
|
+
function addCustomProvider(platform, name, baseUrl, defaultModel = '') {
|
|
207
219
|
db.prepare('INSERT INTO custom_providers (platform, name, base_url, default_model) VALUES (?, ?, ?, ?)').run(platform, name, baseUrl, defaultModel);
|
|
220
|
+
// Register the default model right away so the provider is usable even when
|
|
221
|
+
// its /models endpoint is missing or fails. When no default is given the
|
|
222
|
+
// model list is filled in by the /models sync instead.
|
|
223
|
+
if (defaultModel) {
|
|
224
|
+
upsertModelsFromList(platform, [{ id: defaultModel }]);
|
|
225
|
+
}
|
|
208
226
|
}
|
|
209
227
|
function deleteCustomProvider(platform) {
|
|
210
228
|
db.prepare('DELETE FROM custom_providers WHERE platform = ?').run(platform);
|
|
@@ -218,8 +236,12 @@ function logRequest(data) {
|
|
|
218
236
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
219
237
|
`).run(data.platform, data.model_id, data.prompt_tokens, data.complete_tokens, data.latency_ms, data.success ? 1 : 0, data.error_code ?? null);
|
|
220
238
|
}
|
|
221
|
-
function
|
|
222
|
-
const
|
|
239
|
+
function clearAnalytics() {
|
|
240
|
+
const result = db.prepare('DELETE FROM analytics').run();
|
|
241
|
+
return Number(result.changes);
|
|
242
|
+
}
|
|
243
|
+
function getAnalytics(windowHours = 24) {
|
|
244
|
+
const since = Math.floor(Date.now() / 1000) - windowHours * 3600;
|
|
223
245
|
const rows = db
|
|
224
246
|
.prepare('SELECT * FROM analytics WHERE ts >= ?')
|
|
225
247
|
.all(since);
|
|
@@ -248,14 +270,6 @@ function getAnalytics(windowHours) {
|
|
|
248
270
|
byPlatform,
|
|
249
271
|
};
|
|
250
272
|
}
|
|
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
|
-
}
|
|
259
273
|
// ─── Sessions ─────────────────────────────────────────────────────────────────
|
|
260
274
|
function createSession(token, ttlSeconds = 86400 * 7) {
|
|
261
275
|
const expires = Math.floor(Date.now() / 1000) + ttlSeconds;
|
package/dist/index.js
CHANGED
|
@@ -92,7 +92,7 @@ async function startServer(config = {}) {
|
|
|
92
92
|
else {
|
|
93
93
|
app.get('/', (_req, res) => {
|
|
94
94
|
res.send(`<html><body style="font-family:sans-serif;padding:2rem">
|
|
95
|
-
<h1>
|
|
95
|
+
<h1>FreeLLM is running</h1>
|
|
96
96
|
<p>API endpoint: <code>http://localhost:${port}/v1</code></p>
|
|
97
97
|
<p>Dashboard not built. Run <code>npm run build:client</code> to build the UI.</p>
|
|
98
98
|
</body></html>`);
|
|
@@ -11,5 +11,5 @@ export declare function getProvider(platform: string, type: 'chat'): BaseProvide
|
|
|
11
11
|
export declare function getProvider(platform: string, type: 'image'): BaseImageProvider | undefined;
|
|
12
12
|
export declare function getProvider(platform: string, type: 'tts'): BaseTTSProvider | undefined;
|
|
13
13
|
export declare function allProviders(type?: 'chat' | 'image' | 'tts'): string[];
|
|
14
|
-
export declare function registerCustomProvider(platform: string, baseUrl: string, defaultModel
|
|
14
|
+
export declare function registerCustomProvider(platform: string, baseUrl: string, defaultModel?: string): void;
|
|
15
15
|
export declare function unregisterCustomProvider(platform: string): void;
|
package/dist/providers/index.js
CHANGED
|
@@ -56,7 +56,7 @@ function allProviders(type = 'chat') {
|
|
|
56
56
|
return Object.keys(ttsRegistry);
|
|
57
57
|
return Object.keys(chatRegistry);
|
|
58
58
|
}
|
|
59
|
-
function registerCustomProvider(platform, baseUrl, defaultModel) {
|
|
59
|
+
function registerCustomProvider(platform, baseUrl, defaultModel = '') {
|
|
60
60
|
chatRegistry[platform] = new openai_compat_1.OpenAICompatProvider(platform, baseUrl, defaultModel);
|
|
61
61
|
}
|
|
62
62
|
function unregisterCustomProvider(platform) {
|
|
@@ -3,7 +3,7 @@ export declare class OpenAICompatProvider extends BaseProvider {
|
|
|
3
3
|
readonly platform: string;
|
|
4
4
|
readonly baseUrl: string;
|
|
5
5
|
private _defaultModel;
|
|
6
|
-
constructor(platform: string, baseUrl: string, defaultModel
|
|
6
|
+
constructor(platform: string, baseUrl: string, defaultModel?: string);
|
|
7
7
|
protected defaultModel(): string;
|
|
8
8
|
listModels(apiKey: string): Promise<Array<{
|
|
9
9
|
id: string;
|
|
@@ -4,7 +4,7 @@ exports.PROVIDERS = exports.OpenAICompatProvider = void 0;
|
|
|
4
4
|
// Generic OpenAI-compatible provider (Groq, Cerebras, Mistral, GitHub, Cohere, LLM7, OVH, OpenRouter, etc.)
|
|
5
5
|
const types_1 = require("./types");
|
|
6
6
|
class OpenAICompatProvider extends types_1.BaseProvider {
|
|
7
|
-
constructor(platform, baseUrl, defaultModel) {
|
|
7
|
+
constructor(platform, baseUrl, defaultModel = '') {
|
|
8
8
|
super();
|
|
9
9
|
this.platform = platform;
|
|
10
10
|
this.baseUrl = baseUrl;
|
|
@@ -96,7 +96,11 @@ export declare abstract class BaseProvider {
|
|
|
96
96
|
protected fetchJSON<T>(url: string, options: RequestInit): Promise<T>;
|
|
97
97
|
abstract chat(apiKey: string, req: ChatRequest): Promise<ChatResponse>;
|
|
98
98
|
abstract streamChat(apiKey: string, req: ChatRequest, onChunk: (chunk: StreamChunk) => void, onDone: () => void, onError: (err: Error) => void): Promise<void>;
|
|
99
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* Verify the provider is reachable. Prefer the /models endpoint because it is
|
|
101
|
+
* cheap, then fall back to a 1-token chat request for providers that do not
|
|
102
|
+
* expose a model list (or return an empty one).
|
|
103
|
+
*/
|
|
100
104
|
healthCheck(apiKey: string): Promise<boolean>;
|
|
101
105
|
/** Fetch available models from the provider. Default: none (provider does not expose a list). */
|
|
102
106
|
listModels(_apiKey: string): Promise<Array<{
|
package/dist/providers/types.js
CHANGED
|
@@ -13,9 +13,35 @@ class BaseProvider {
|
|
|
13
13
|
}
|
|
14
14
|
return res.json();
|
|
15
15
|
}
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Verify the provider is reachable. Prefer the /models endpoint because it is
|
|
18
|
+
* cheap, then fall back to a 1-token chat request for providers that do not
|
|
19
|
+
* expose a model list (or return an empty one).
|
|
20
|
+
*/
|
|
17
21
|
async healthCheck(apiKey) {
|
|
18
|
-
|
|
22
|
+
let listError;
|
|
23
|
+
try {
|
|
24
|
+
const models = await this.listModels(apiKey);
|
|
25
|
+
if (models.length > 0)
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
const status = e.status;
|
|
30
|
+
if (status === 401 || status === 403)
|
|
31
|
+
throw e;
|
|
32
|
+
listError = e;
|
|
33
|
+
}
|
|
34
|
+
const fallbackModel = this.defaultModel();
|
|
35
|
+
if (!fallbackModel) {
|
|
36
|
+
if (listError)
|
|
37
|
+
throw listError;
|
|
38
|
+
throw new Error('Provider returned no models and no default model is configured');
|
|
39
|
+
}
|
|
40
|
+
await this.chat(apiKey, {
|
|
41
|
+
model: fallbackModel,
|
|
42
|
+
messages: [{ role: 'user', content: 'hi' }],
|
|
43
|
+
max_tokens: 1,
|
|
44
|
+
});
|
|
19
45
|
return true;
|
|
20
46
|
}
|
|
21
47
|
/** Fetch available models from the provider. Default: none (provider does not expose a list). */
|
|
@@ -5,13 +5,9 @@ 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) ||
|
|
8
|
+
const hours = Number(req.query.hours) || 24;
|
|
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
11
|
exports.analyticsRouter.delete('/analytics', (_req, res) => {
|
|
15
|
-
(0, db_1.clearAnalytics)();
|
|
16
|
-
return res.json({ ok: true });
|
|
12
|
+
return res.json({ ok: true, deleted: (0, db_1.clearAnalytics)() });
|
|
17
13
|
});
|
|
@@ -22,20 +22,53 @@ exports.customProvidersRouter.get('/custom-providers', (_req, res) => {
|
|
|
22
22
|
});
|
|
23
23
|
exports.customProvidersRouter.post('/custom-providers', (req, res) => {
|
|
24
24
|
const { name, baseUrl, defaultModel, apiKey } = req.body;
|
|
25
|
-
if (!name || !baseUrl
|
|
26
|
-
return res.status(400).json({ error: 'name
|
|
25
|
+
if (!name || !baseUrl) {
|
|
26
|
+
return res.status(400).json({ error: 'name and baseUrl are required' });
|
|
27
27
|
}
|
|
28
28
|
const platform = slugify(name);
|
|
29
29
|
if (!platform) {
|
|
30
30
|
return res.status(400).json({ error: 'provider name must contain letters or numbers' });
|
|
31
31
|
}
|
|
32
|
+
if ((0, db_1.getCustomProvider)(platform)) {
|
|
33
|
+
return res.status(409).json({ error: `provider '${name}' already exists` });
|
|
34
|
+
}
|
|
35
|
+
// Never let a custom provider shadow a built-in one — that would replace its
|
|
36
|
+
// models and keys with an unrelated endpoint.
|
|
37
|
+
if ((0, providers_1.allProviders)('chat').includes(platform) ||
|
|
38
|
+
(0, providers_1.allProviders)('image').includes(platform) ||
|
|
39
|
+
(0, providers_1.allProviders)('tts').includes(platform)) {
|
|
40
|
+
return res.status(409).json({ error: `provider name '${name}' conflicts with a built-in provider` });
|
|
41
|
+
}
|
|
42
|
+
let normalizedBaseUrl = baseUrl.trim();
|
|
43
|
+
if (!normalizedBaseUrl) {
|
|
44
|
+
return res.status(400).json({ error: 'baseUrl is required' });
|
|
45
|
+
}
|
|
46
|
+
const hasHttpScheme = /^https?:\/\//i.test(normalizedBaseUrl);
|
|
47
|
+
if (!hasHttpScheme && /^[a-z][a-z0-9+.-]*:\/\//i.test(normalizedBaseUrl)) {
|
|
48
|
+
return res.status(400).json({ error: 'baseUrl must use http or https' });
|
|
49
|
+
}
|
|
50
|
+
if (!hasHttpScheme) {
|
|
51
|
+
normalizedBaseUrl = `http://${normalizedBaseUrl}`;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const parsed = new URL(normalizedBaseUrl);
|
|
55
|
+
if (!parsed.hostname)
|
|
56
|
+
throw new Error('missing hostname');
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return res.status(400).json({ error: 'baseUrl must be a valid http(s) URL' });
|
|
60
|
+
}
|
|
61
|
+
normalizedBaseUrl = normalizedBaseUrl.replace(/\/+$/, '');
|
|
62
|
+
// Optional: providers that expose /models have their models imported by the
|
|
63
|
+
// sync step. A default model is only needed for providers without /models.
|
|
64
|
+
const normalizedModel = typeof defaultModel === 'string' ? defaultModel.trim() : '';
|
|
32
65
|
try {
|
|
33
|
-
(0, db_1.addCustomProvider)(platform, name.trim(),
|
|
66
|
+
(0, db_1.addCustomProvider)(platform, name.trim(), normalizedBaseUrl, normalizedModel);
|
|
34
67
|
}
|
|
35
68
|
catch {
|
|
36
69
|
return res.status(409).json({ error: `provider '${name}' already exists` });
|
|
37
70
|
}
|
|
38
|
-
(0, providers_1.registerCustomProvider)(platform,
|
|
71
|
+
(0, providers_1.registerCustomProvider)(platform, normalizedBaseUrl, normalizedModel);
|
|
39
72
|
// Always create a key row (empty key for providers without auth) so the
|
|
40
73
|
// platform can sync models and appear in /v1/models
|
|
41
74
|
const keyId = (0, db_1.addKey)(platform, (apiKey ?? '').trim(), name.trim());
|
package/dist/routes/api/keys.js
CHANGED
|
@@ -1,28 +1,24 @@
|
|
|
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
|
exports.keysRouter = void 0;
|
|
7
4
|
const express_1 = require("express");
|
|
8
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
9
5
|
const db_1 = require("../../db");
|
|
10
6
|
const db_2 = require("../../db");
|
|
11
|
-
const
|
|
7
|
+
const crypto_1 = require("../../crypto");
|
|
12
8
|
const providers_1 = require("../../providers");
|
|
13
9
|
exports.keysRouter = (0, express_1.Router)();
|
|
14
10
|
// Get unified API key (or generate one)
|
|
15
11
|
exports.keysRouter.get('/keys/unified', (_req, res) => {
|
|
16
12
|
let key = (0, db_2.getSetting)('unified_key');
|
|
17
13
|
if (!key) {
|
|
18
|
-
key = (0,
|
|
14
|
+
key = (0, crypto_1.generateUnifiedKey)();
|
|
19
15
|
(0, db_2.setSetting)('unified_key', key);
|
|
20
16
|
}
|
|
21
17
|
return res.json({ key });
|
|
22
18
|
});
|
|
23
19
|
// Regenerate unified key
|
|
24
20
|
exports.keysRouter.post('/keys/unified/regenerate', (_req, res) => {
|
|
25
|
-
const key = (0,
|
|
21
|
+
const key = (0, crypto_1.generateUnifiedKey)();
|
|
26
22
|
(0, db_2.setSetting)('unified_key', key);
|
|
27
23
|
return res.json({ key });
|
|
28
24
|
});
|
|
@@ -51,13 +47,10 @@ exports.keysRouter.get('/keys', (_req, res) => {
|
|
|
51
47
|
// Add provider key
|
|
52
48
|
exports.keysRouter.post('/keys', (req, res) => {
|
|
53
49
|
const { platform, key, label } = req.body;
|
|
54
|
-
if (!platform) {
|
|
55
|
-
return res.status(400).json({ error: 'platform
|
|
50
|
+
if (!platform || !key) {
|
|
51
|
+
return res.status(400).json({ error: 'platform and key are required' });
|
|
56
52
|
}
|
|
57
|
-
|
|
58
|
-
return res.status(400).json({ error: 'key is required for this platform' });
|
|
59
|
-
}
|
|
60
|
-
const id = (0, db_1.addKey)(platform, key ?? '', label);
|
|
53
|
+
const id = (0, db_1.addKey)(platform, key, label);
|
|
61
54
|
return res.json({ id });
|
|
62
55
|
});
|
|
63
56
|
// Delete provider key
|
|
@@ -123,76 +116,31 @@ exports.keysRouter.post('/keys/:id/models/sync', async (req, res) => {
|
|
|
123
116
|
return res.status(500).json({ error: e.message });
|
|
124
117
|
}
|
|
125
118
|
});
|
|
126
|
-
|
|
127
|
-
|
|
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) => {
|
|
119
|
+
// Verify the provider's current model list and disable stored models it no longer exposes.
|
|
120
|
+
exports.keysRouter.post('/keys/:id/models/verify', async (req, res) => {
|
|
131
121
|
const k = (0, db_1.getKey)(Number(req.params.id));
|
|
132
122
|
if (!k)
|
|
133
123
|
return res.status(404).json({ error: 'key not found' });
|
|
134
124
|
const provider = (0, providers_1.getChatProvider)(k.platform);
|
|
135
125
|
if (!provider)
|
|
136
126
|
return res.status(400).json({ error: `no provider for platform '${k.platform}'` });
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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);
|
|
127
|
+
try {
|
|
128
|
+
const rawKey = (0, db_1.decryptKey)(k.key_enc);
|
|
129
|
+
const remote = await provider.listModels(rawKey);
|
|
130
|
+
if (remote.length === 0) {
|
|
131
|
+
return res.status(502).json({ error: 'provider returned no available models; no models were disabled' });
|
|
182
132
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
available: job.available,
|
|
197
|
-
});
|
|
133
|
+
const availability = (0, db_1.updateModelsAvailability)(k.platform, remote.map(m => m.id));
|
|
134
|
+
return res.json({
|
|
135
|
+
ok: true,
|
|
136
|
+
available: remote.length,
|
|
137
|
+
enabled: availability.enabled.length,
|
|
138
|
+
disabled: availability.disabled.length,
|
|
139
|
+
enabledModelIds: availability.enabled,
|
|
140
|
+
disabledModelIds: availability.disabled,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
catch (e) {
|
|
144
|
+
return res.status(502).json({ error: e.message });
|
|
145
|
+
}
|
|
198
146
|
});
|
|
@@ -17,7 +17,7 @@ exports.modelsApiRouter.get('/models', (req, res) => {
|
|
|
17
17
|
if (key.enabled)
|
|
18
18
|
keyedPlatforms.add(key.platform);
|
|
19
19
|
}
|
|
20
|
-
return res.json(all.filter((m) => keyedPlatforms.has(m.platform)));
|
|
20
|
+
return res.json(all.filter((m) => m.enabled && keyedPlatforms.has(m.platform)));
|
|
21
21
|
});
|
|
22
22
|
exports.modelsApiRouter.patch('/models/:id/toggle', (req, res) => {
|
|
23
23
|
const { enabled } = req.body;
|