-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
673 lines (616 loc) · 19.7 KB
/
Copy pathserver.js
File metadata and controls
673 lines (616 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
const http = require("node:http");
const fs = require("node:fs");
const path = require("node:path");
const { validateDetectedProvider } = require("./lib/providerRegistry");
const HOST = process.env.HOST || "0.0.0.0";
const PORT = Number(process.env.PORT || 8099);
const PUBLIC_DIR = path.join(__dirname, "public");
const PROVIDERS = {
google: {
label: "Google",
catalog: [
{ id: "gemini-2.5-flash", label: "Gemini 2.5 Flash" },
{ id: "gemini-2.5-pro", label: "Gemini 2.5 Pro" },
{ id: "gemini-2.0-flash", label: "Gemini 2.0 Flash" },
{ id: "gemini-2.0-flash-001", label: "Gemini 2.0 Flash 001" },
{ id: "gemini-2.0-flash-lite-001", label: "Gemini 2.0 Flash-Lite 001" },
{ id: "gemini-2.0-flash-lite", label: "Gemini 2.0 Flash-Lite" },
{ id: "gemma-3-1b-it", label: "Gemma 3 1B" },
{ id: "gemma-3-4b-it", label: "Gemma 3 4B" },
{ id: "gemma-3-12b-it", label: "Gemma 3 12B" },
{ id: "gemma-3-27b-it", label: "Gemma 3 27B" }
]
},
openai: {
label: "OpenAI",
catalog: [
{ id: "gpt-5.5", label: "GPT-5.5" },
{ id: "gpt-5.4", label: "GPT-5.4" },
{ id: "gpt-5", label: "GPT-5" },
{ id: "gpt-4.1", label: "GPT-4.1" },
{ id: "o3", label: "o3" },
{ id: "o4-mini", label: "o4 mini" }
]
},
anthropic: {
label: "Anthropic",
catalog: [
{ id: "claude-opus-4-5", label: "Claude Opus 4.5" },
{ id: "claude-sonnet-4-5", label: "Claude Sonnet 4.5" },
{ id: "claude-haiku-4-5", label: "Claude Haiku 4.5" },
{ id: "claude-3-7-sonnet-latest", label: "Claude 3.7 Sonnet Latest" },
{ id: "claude-3-5-haiku-latest", label: "Claude 3.5 Haiku Latest" }
]
},
deepseek: {
label: "DeepSeek",
catalog: [
{ id: "deepseek-v4-flash", label: "DeepSeek V4 Flash" },
{ id: "deepseek-v4-pro", label: "DeepSeek V4 Pro" },
{ id: "deepseek-chat", label: "DeepSeek Chat" },
{ id: "deepseek-reasoner", label: "DeepSeek Reasoner" }
]
},
groq: {
label: "Groq",
catalog: [
{ id: "llama-3.3-70b-versatile", label: "Llama 3.3 70B Versatile" },
{ id: "llama-3.1-8b-instant", label: "Llama 3.1 8B Instant" },
{ id: "moonshotai/kimi-k2-instruct", label: "Kimi K2 Instruct" },
{ id: "openai/gpt-oss-120b", label: "GPT OSS 120B" }
]
},
xai: {
label: "X",
catalog: [
{ id: "latest", label: "Grok Latest" },
{ id: "grok-4.3", label: "Grok 4.3" },
{ id: "grok-4.3-fast", label: "Grok 4.3 Fast" },
{ id: "grok-420-reasoning", label: "Grok 420 Reasoning" }
]
}
};
const MIME_TYPES = {
".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".svg": "image/svg+xml",
".ico": "image/x-icon"
};
function normalizeModelName(name) {
return String(name || "").replace(/^models\//, "");
}
function sendJson(res, statusCode, payload) {
res.writeHead(statusCode, {
"content-type": "application/json; charset=utf-8",
"cache-control": "no-store"
});
res.end(JSON.stringify(payload));
}
function sendText(res, statusCode, message) {
res.writeHead(statusCode, { "content-type": "text/plain; charset=utf-8" });
res.end(message);
}
function safeFilePath(urlPath) {
const cleanPath = urlPath === "/" ? "/index.html" : urlPath;
const decodedPath = decodeURIComponent(cleanPath.split("?")[0]);
const filePath = path.normalize(path.join(PUBLIC_DIR, decodedPath));
if (!filePath.startsWith(PUBLIC_DIR)) return null;
return filePath;
}
function readJsonBody(req) {
return new Promise((resolve, reject) => {
let raw = "";
req.on("data", (chunk) => {
raw += chunk;
if (raw.length > 1_000_000) {
reject(new Error("Request body too large"));
req.destroy();
}
});
req.on("end", () => {
try {
resolve(raw ? JSON.parse(raw) : {});
} catch {
reject(new Error("Invalid JSON"));
}
});
req.on("error", reject);
});
}
function getProvider(body) {
const provider = String(body.provider || "").trim().toLowerCase();
if (!PROVIDERS[provider]) {
const error = new Error("Unsupported provider");
error.statusCode = 400;
throw error;
}
return provider;
}
function getApiKey(body) {
const apiKey = String(body.apiKey || "").trim();
if (!apiKey) {
const error = new Error("API key is required");
error.statusCode = 400;
throw error;
}
return apiKey;
}
function getModelId(value) {
const model = normalizeModelName(value).trim();
if (!/^[A-Za-z0-9._:-]+$/.test(model)) {
const error = new Error("Invalid model id");
error.statusCode = 400;
throw error;
}
return model;
}
async function apiFetch(provider, url, options = {}) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), Number(process.env.PROVIDER_TIMEOUT_MS || 9000));
let response;
try {
response = await fetch(url, {
...options,
signal: controller.signal,
headers: {
"content-type": "application/json",
...(options.headers || {})
}
});
} catch (error) {
const wrapped = new Error(error.name === "AbortError"
? `${PROVIDERS[provider].label} API request timed out`
: `${PROVIDERS[provider].label} API request failed`);
wrapped.statusCode = error.name === "AbortError" ? 504 : 502;
throw wrapped;
} finally {
clearTimeout(timeout);
}
const text = await response.text();
let data = {};
try {
data = text ? JSON.parse(text) : {};
} catch {
data = { raw: text };
}
if (!response.ok) {
const message =
data?.error?.message ||
data?.error?.error?.message ||
data?.message ||
`${PROVIDERS[provider].label} API request failed with ${response.status}`;
const error = new Error(message);
error.statusCode = response.status;
error.details = data;
throw error;
}
return data;
}
function mergeCatalog(provider, apiModels, options = {}) {
const seen = new Set();
const modelMap = new Map(apiModels.map((model) => [model.id, model]));
const models = [];
for (const item of PROVIDERS[provider].catalog) {
const apiModel = modelMap.get(item.id);
seen.add(item.id);
models.push({
id: item.id,
label: apiModel?.label || item.label,
available: Boolean(apiModel?.available),
methods: apiModel?.methods || [],
owner: apiModel?.owner || "",
source: apiModel ? "api" : "catalog",
chatCapable: Boolean(apiModel?.available),
type: "model"
});
}
for (const apiModel of apiModels) {
if (seen.has(apiModel.id)) continue;
models.push({
id: apiModel.id,
label: apiModel.label || apiModel.id,
available: Boolean(apiModel.available),
methods: apiModel.methods || [],
owner: apiModel.owner || "",
source: "api",
chatCapable: Boolean(apiModel.available),
type: "model"
});
}
if (options.sortAvailableFirst) {
models.sort((a, b) => Number(b.available) - Number(a.available) || a.label.localeCompare(b.label));
}
return models;
}
async function listGoogleModels(apiKey) {
const data = await apiFetch("google", `https://generativelanguage.googleapis.com/v1beta/models?key=${encodeURIComponent(apiKey)}`);
const apiModels = (Array.isArray(data.models) ? data.models : []).map((model) => {
const methods = model.supportedGenerationMethods || [];
return {
id: normalizeModelName(model.name),
label: model.displayName || normalizeModelName(model.name),
available: methods.includes("generateContent"),
methods,
owner: "google"
};
});
return mergeCatalog("google", apiModels);
}
async function listOpenAIModels(apiKey) {
const data = await apiFetch("openai", "https://api.openai.com/v1/models", {
headers: { authorization: `Bearer ${apiKey}` }
});
const apiModels = (Array.isArray(data.data) ? data.data : [])
.filter((model) => !/(embedding|image|audio|tts|whisper|moderation|transcribe)/i.test(model.id || ""))
.map((model) => ({
id: model.id,
label: model.id,
available: true,
methods: ["responses"],
owner: model.owned_by || "openai"
}));
return mergeCatalog("openai", apiModels, { sortAvailableFirst: true });
}
async function listAnthropicModels(apiKey) {
const data = await apiFetch("anthropic", "https://api.anthropic.com/v1/models", {
headers: {
"x-api-key": apiKey,
"anthropic-version": "2023-06-01"
}
});
const apiModels = (Array.isArray(data.data) ? data.data : []).map((model) => ({
id: model.id,
label: model.display_name || model.id,
available: true,
methods: ["messages"],
owner: "anthropic"
}));
return mergeCatalog("anthropic", apiModels, { sortAvailableFirst: true });
}
async function listDeepSeekModels(apiKey) {
const data = await apiFetch("deepseek", "https://api.deepseek.com/models", {
headers: { authorization: `Bearer ${apiKey}` }
});
const apiModels = (Array.isArray(data.data) ? data.data : []).map((model) => ({
id: model.id,
label: model.id,
available: true,
methods: ["chat.completions"],
owner: model.owned_by || "deepseek"
}));
return mergeCatalog("deepseek", apiModels);
}
async function listGroqModels(apiKey) {
const data = await apiFetch("groq", "https://api.groq.com/openai/v1/models", {
headers: { authorization: `Bearer ${apiKey}` }
});
const apiModels = (Array.isArray(data.data) ? data.data : []).map((model) => ({
id: model.id,
label: model.id,
available: true,
methods: ["chat.completions"],
owner: model.owned_by || "groq"
}));
return mergeCatalog("groq", apiModels, { sortAvailableFirst: true });
}
async function listXAIModels(apiKey) {
const data = await apiFetch("xai", "https://api.x.ai/v1/models", {
headers: { authorization: `Bearer ${apiKey}` }
});
const apiModels = (Array.isArray(data.data) ? data.data : [])
.filter((model) => !/(image|video)/i.test(model.id || ""))
.map((model) => ({
id: model.id,
label: model.id,
available: true,
methods: ["chat.completions"],
owner: model.owned_by || "xai"
}));
return mergeCatalog("xai", apiModels, { sortAvailableFirst: true });
}
function providerDetectionOrder(apiKey) {
const key = apiKey.toLowerCase();
const preferred = ["google"];
if (key.startsWith("sk-ant-")) preferred.push("anthropic");
if (key.startsWith("sk-")) preferred.push("openai", "deepseek", "anthropic");
if (key.startsWith("ds-")) preferred.push("deepseek");
if (key.startsWith("gsk_")) preferred.push("groq");
if (key.startsWith("xai-")) preferred.push("xai");
preferred.push("openai", "anthropic", "deepseek", "groq", "xai");
return [...new Set(preferred)];
}
async function providerRawRequest(url, options = {}) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), Number(process.env.PROVIDER_TIMEOUT_MS || 9000));
try {
const response = await fetch(url, {
...options,
signal: controller.signal,
headers: {
"content-type": "application/json",
...(options.headers || {})
}
});
const text = await response.text();
let data = {};
try {
data = text ? JSON.parse(text) : {};
} catch {
data = {};
}
return {
ok: response.ok,
status: response.status,
data,
text
};
} catch (error) {
return {
ok: false,
status: error.name === "AbortError" ? 504 : 0,
data: {},
text: error.message || "request failed"
};
} finally {
clearTimeout(timeout);
}
}
async function detectProviderModels(apiKey) {
const listModels = {
google: listGoogleModels,
openai: listOpenAIModels,
anthropic: listAnthropicModels,
deepseek: listDeepSeekModels,
groq: listGroqModels,
xai: listXAIModels
};
const attempts = [];
for (const provider of providerDetectionOrder(apiKey)) {
try {
const models = await listModels[provider](apiKey);
return {
provider,
providerLabel: PROVIDERS[provider].label,
models,
checked: [...attempts.map((attempt) => attempt.provider), provider]
};
} catch (error) {
attempts.push({
provider,
statusCode: error.statusCode || 500,
message: error.message
});
}
}
const detected = await validateDetectedProvider(apiKey, providerRawRequest);
if (detected) {
return {
...detected,
checked: [...attempts.map((attempt) => attempt.provider), detected.provider]
};
}
const error = new Error(
`Could not identify this key. Checked: ${attempts.map((attempt) => PROVIDERS[attempt.provider].label).join(", ")}.`
);
error.statusCode = attempts.find((attempt) => attempt.statusCode === 401)?.statusCode || 400;
error.attempts = attempts;
throw error;
}
async function handleModels(req, res) {
try {
const body = await readJsonBody(req);
const apiKey = getApiKey(body);
const result = await detectProviderModels(apiKey);
sendJson(res, 200, {
provider: result.provider,
providerLabel: result.providerLabel,
checked: result.checked,
models: result.models
});
} catch (error) {
sendJson(res, error.statusCode || 500, {
error: error.message || "Unable to check model access",
attempts: error.attempts || undefined
});
}
}
function safeHistory(history) {
return (Array.isArray(history) ? history : [])
.slice(-24)
.map((entry) => ({
role: entry?.role === "assistant" || entry?.role === "model" ? "assistant" : "user",
text: String(entry?.text || "").trim().slice(0, 12000)
}))
.filter((entry) => entry.text);
}
function googleContents(history, message) {
const contents = [];
for (const entry of safeHistory(history)) {
contents.push({
role: entry.role === "assistant" ? "model" : "user",
parts: [{ text: entry.text }]
});
}
contents.push({ role: "user", parts: [{ text: String(message).slice(0, 12000) }] });
return contents;
}
function chatMessages(history, message) {
return [
...safeHistory(history).map((entry) => ({ role: entry.role, content: entry.text })),
{ role: "user", content: String(message).slice(0, 12000) }
];
}
function extractGoogleReply(data) {
const parts = data?.candidates?.[0]?.content?.parts || [];
const text = parts.map((part) => part.text || "").join("").trim();
if (text) return text;
const finishReason = data?.candidates?.[0]?.finishReason;
return finishReason ? `No text returned. Finish reason: ${finishReason}` : "No text returned.";
}
function extractOpenAIReply(data) {
if (typeof data.output_text === "string" && data.output_text.trim()) return data.output_text.trim();
const chunks = [];
for (const item of data.output || []) {
for (const content of item.content || []) {
if (content.type === "output_text" && content.text) chunks.push(content.text);
if (content.type === "text" && content.text) chunks.push(content.text);
}
}
return chunks.join("").trim() || "No text returned.";
}
function extractAnthropicReply(data) {
const chunks = (data.content || [])
.filter((part) => part.type === "text" && part.text)
.map((part) => part.text);
return chunks.join("").trim() || "No text returned.";
}
function extractChatCompletionReply(data) {
return data?.choices?.[0]?.message?.content?.trim() || "No text returned.";
}
async function chatGoogle(apiKey, model, history, message) {
const url = `https://generativelanguage.googleapis.com/v1beta/models/${encodeURIComponent(model)}:generateContent?key=${encodeURIComponent(apiKey)}`;
const data = await apiFetch("google", url, {
method: "POST",
body: JSON.stringify({
contents: googleContents(history, message),
generationConfig: { temperature: 0.7, topP: 0.95, maxOutputTokens: 4096 }
})
});
return extractGoogleReply(data);
}
async function chatOpenAI(apiKey, model, history, message) {
const data = await apiFetch("openai", "https://api.openai.com/v1/responses", {
method: "POST",
headers: { authorization: `Bearer ${apiKey}` },
body: JSON.stringify({
model,
input: chatMessages(history, message),
max_output_tokens: 4096,
store: false
})
});
return extractOpenAIReply(data);
}
async function chatAnthropic(apiKey, model, history, message) {
const data = await apiFetch("anthropic", "https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"x-api-key": apiKey,
"anthropic-version": "2023-06-01"
},
body: JSON.stringify({
model,
max_tokens: 4096,
messages: chatMessages(history, message)
})
});
return extractAnthropicReply(data);
}
async function chatDeepSeek(apiKey, model, history, message) {
const data = await apiFetch("deepseek", "https://api.deepseek.com/chat/completions", {
method: "POST",
headers: { authorization: `Bearer ${apiKey}` },
body: JSON.stringify({
model,
messages: chatMessages(history, message),
temperature: 0.7,
max_tokens: 4096,
stream: false
})
});
return extractChatCompletionReply(data);
}
async function chatGroq(apiKey, model, history, message) {
const data = await apiFetch("groq", "https://api.groq.com/openai/v1/chat/completions", {
method: "POST",
headers: { authorization: `Bearer ${apiKey}` },
body: JSON.stringify({
model,
messages: chatMessages(history, message),
temperature: 0.7,
max_tokens: 4096,
stream: false
})
});
return extractChatCompletionReply(data);
}
async function chatXAI(apiKey, model, history, message) {
const data = await apiFetch("xai", "https://api.x.ai/v1/chat/completions", {
method: "POST",
headers: { authorization: `Bearer ${apiKey}` },
body: JSON.stringify({
model,
messages: chatMessages(history, message),
temperature: 0.7,
max_tokens: 4096,
stream: false
})
});
return extractChatCompletionReply(data);
}
async function handleChat(req, res) {
try {
const body = await readJsonBody(req);
const provider = getProvider(body);
const apiKey = getApiKey(body);
const model = getModelId(body.model);
const message = String(body.message || "").trim();
if (!message) {
sendJson(res, 400, { error: "Message is required" });
return;
}
const chat = {
google: chatGoogle,
openai: chatOpenAI,
anthropic: chatAnthropic,
deepseek: chatDeepSeek,
groq: chatGroq,
xai: chatXAI
}[provider];
const reply = await chat(apiKey, model, body.history, message);
sendJson(res, 200, { provider, reply });
} catch (error) {
sendJson(res, error.statusCode || 500, {
error: error.message || "Unable to send chat message"
});
}
}
function serveStatic(req, res) {
const filePath = safeFilePath(req.url);
if (!filePath) {
sendText(res, 403, "Forbidden");
return;
}
fs.readFile(filePath, (error, data) => {
if (error) {
sendText(res, error.code === "ENOENT" ? 404 : 500, error.code === "ENOENT" ? "Not found" : "Server error");
return;
}
const ext = path.extname(filePath);
res.writeHead(200, {
"content-type": MIME_TYPES[ext] || "application/octet-stream",
"cache-control": "no-store"
});
res.end(req.method === "HEAD" ? undefined : data);
});
}
const server = http.createServer((req, res) => {
if (req.method === "POST" && req.url === "/api/models") {
handleModels(req, res);
return;
}
if (req.method === "POST" && req.url === "/api/chat") {
handleChat(req, res);
return;
}
if (req.method === "GET" || req.method === "HEAD") {
serveStatic(req, res);
return;
}
sendText(res, 405, "Method not allowed");
});
server.listen(PORT, HOST, () => {
console.log(`APIKeyValidator listening on http://${HOST}:${PORT}`);
});