Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,184 @@
|
||||
"use strict";
|
||||
|
||||
const AI_PROVIDER_CONFIGS = {
|
||||
openai: {
|
||||
name: 'OpenAI',
|
||||
keyEnv: 'OPENAI_API_KEY',
|
||||
requiresKey: true,
|
||||
chatProtocol: 'openai-compatible',
|
||||
chatEndpoint: 'https://api.openai.com/v1/chat/completions',
|
||||
modelDiscovery: 'openai-style',
|
||||
modelListUrls: ['https://api.openai.com/v1/models']
|
||||
},
|
||||
anthropic: {
|
||||
name: 'Anthropic',
|
||||
keyEnv: 'ANTHROPIC_API_KEY',
|
||||
requiresKey: true,
|
||||
chatProtocol: 'anthropic',
|
||||
chatEndpoint: 'https://api.anthropic.com/v1/messages',
|
||||
modelDiscovery: 'anthropic',
|
||||
modelListUrls: ['https://api.anthropic.com/v1/models']
|
||||
},
|
||||
'openai-compatible': {
|
||||
name: 'OpenAI Compatible',
|
||||
keyEnv: 'OPENAI_COMPATIBLE_API_KEY',
|
||||
baseUrlEnv: 'OPENAI_COMPATIBLE_BASE_URL',
|
||||
modelEnv: 'OPENAI_COMPATIBLE_MODEL_ID',
|
||||
modelIdsEnv: 'OPENAI_COMPATIBLE_MODEL_IDS',
|
||||
requiresKey: false,
|
||||
requiresBaseUrl: true,
|
||||
chatProtocol: 'openai-compatible',
|
||||
modelDiscovery: 'openai-compatible'
|
||||
},
|
||||
google: {
|
||||
name: 'Google AI',
|
||||
keyEnv: 'GOOGLE_AI_KEY',
|
||||
requiresKey: true,
|
||||
chatProtocol: 'google',
|
||||
modelDiscovery: 'google'
|
||||
},
|
||||
groq: {
|
||||
name: 'Groq',
|
||||
keyEnv: 'GROQ_API_KEY',
|
||||
requiresKey: true,
|
||||
chatProtocol: 'openai-compatible',
|
||||
chatEndpoint: 'https://api.groq.com/openai/v1/chat/completions',
|
||||
modelDiscovery: 'openai-style',
|
||||
modelListUrls: ['https://api.groq.com/openai/v1/models']
|
||||
},
|
||||
huggingface: {
|
||||
name: 'HuggingFace',
|
||||
keyEnv: 'HUGGINGFACE_API_KEY',
|
||||
requiresKey: true,
|
||||
chatProtocol: 'huggingface',
|
||||
chatEndpoint: 'https://router.huggingface.co/v1/chat/completions',
|
||||
modelDiscovery: 'huggingface'
|
||||
},
|
||||
together: {
|
||||
name: 'Together AI',
|
||||
keyEnv: 'TOGETHER_API_KEY',
|
||||
requiresKey: true,
|
||||
chatProtocol: 'openai-compatible',
|
||||
chatEndpoint: 'https://api.together.ai/v1/chat/completions',
|
||||
modelDiscovery: 'together',
|
||||
modelListUrls: ['https://api.together.ai/v1/models']
|
||||
},
|
||||
cohere: {
|
||||
name: 'Cohere',
|
||||
keyEnv: 'COHERE_API_KEY',
|
||||
requiresKey: true,
|
||||
chatProtocol: 'cohere',
|
||||
modelDiscovery: 'cohere'
|
||||
},
|
||||
openrouter: {
|
||||
name: 'OpenRouter',
|
||||
keyEnv: 'OPENROUTER_API_KEY',
|
||||
requiresKey: true,
|
||||
chatProtocol: 'openai-compatible',
|
||||
chatEndpoint: 'https://openrouter.ai/api/v1/chat/completions',
|
||||
modelDiscovery: 'openrouter',
|
||||
modelListUrls: ['https://openrouter.ai/api/v1/models?output_modalities=text']
|
||||
}
|
||||
};
|
||||
|
||||
const ANTHROPIC_API_VERSION = '2023-06-01';
|
||||
|
||||
function getAiProviderConfig(provider) {
|
||||
const id = String(provider || '').trim().toLowerCase();
|
||||
return Object.hasOwn(AI_PROVIDER_CONFIGS, id) ? AI_PROVIDER_CONFIGS[id] : null;
|
||||
}
|
||||
|
||||
function normalizeAiSecretValue(value) {
|
||||
return String(value == null ? '' : value).trim();
|
||||
}
|
||||
|
||||
// Fixed-length placeholder for a stored secret: the mask must not disclose the
|
||||
// real secret's size or characters, so every non-empty key renders identically.
|
||||
// Empty still means "no key set" for frontend truthiness checks.
|
||||
function maskAiSecretValue(value) {
|
||||
const normalized = normalizeAiSecretValue(value);
|
||||
if (!normalized) return '';
|
||||
return '\u2022'.repeat(12);
|
||||
}
|
||||
|
||||
function normalizeOpenAiCompatibleBaseUrl(input) {
|
||||
const raw = normalizeAiSecretValue(input);
|
||||
if (!raw) return '';
|
||||
|
||||
let parsed;
|
||||
try {
|
||||
parsed = new URL(raw);
|
||||
} catch {
|
||||
throw new Error('Please enter a valid Base URL.');
|
||||
}
|
||||
|
||||
if (!['http:', 'https:'].includes(parsed.protocol)) {
|
||||
throw new Error('Base URL must start with http:// or https://.');
|
||||
}
|
||||
if (parsed.username || parsed.password) {
|
||||
throw new Error('Enter credentials in the API key field, not in the Base URL.');
|
||||
}
|
||||
|
||||
parsed.hash = '';
|
||||
parsed.search = '';
|
||||
|
||||
let pathname = parsed.pathname || '';
|
||||
pathname = pathname.replace(/\/+$/, '');
|
||||
pathname = pathname.replace(/\/(?:chat\/completions|completions|responses|models)$/i, '');
|
||||
parsed.pathname = pathname || '/';
|
||||
|
||||
return parsed.toString().replace(/\/$/, '');
|
||||
}
|
||||
|
||||
function buildOpenAiCompatibleBaseUrlCandidates(input) {
|
||||
const normalized = normalizeOpenAiCompatibleBaseUrl(input);
|
||||
const parsed = new URL(normalized);
|
||||
const pathname = parsed.pathname.replace(/\/+$/, '').toLowerCase();
|
||||
const candidates = [normalized];
|
||||
|
||||
if (!/\/v\d+$/.test(pathname) && !/\/openai\/v\d+$/.test(pathname)) {
|
||||
candidates.push(`${normalized}/v1`);
|
||||
}
|
||||
|
||||
return Array.from(new Set(candidates.map((value) => value.replace(/\/+$/, ''))));
|
||||
}
|
||||
|
||||
function appendPathToBaseUrl(baseUrl, path) {
|
||||
return `${String(baseUrl || '').replace(/\/+$/, '')}/${String(path || '').replace(/^\/+/, '')}`;
|
||||
}
|
||||
|
||||
function buildAiProviderAuthHeaders(provider, apiKey) {
|
||||
const headers = {};
|
||||
if (provider === 'anthropic') {
|
||||
if (apiKey) {
|
||||
headers['x-api-key'] = apiKey;
|
||||
}
|
||||
headers['anthropic-version'] = ANTHROPIC_API_VERSION;
|
||||
} else if (apiKey) {
|
||||
headers['Authorization'] = `Bearer ${apiKey}`;
|
||||
}
|
||||
|
||||
if (provider === 'openrouter') {
|
||||
headers['HTTP-Referer'] = process.env.APP_URL || 'https://adpanel.local';
|
||||
headers['X-Title'] = 'ADPanel';
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
function extractAiErrorMessage(response) {
|
||||
const payload = response?.data || {};
|
||||
return payload?.error?.message || payload?.error || payload?.message || payload?.raw || `API error: ${response?.status || 'request failed'}`;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
AI_PROVIDER_CONFIGS,
|
||||
getAiProviderConfig,
|
||||
normalizeAiSecretValue,
|
||||
maskAiSecretValue,
|
||||
normalizeOpenAiCompatibleBaseUrl,
|
||||
buildOpenAiCompatibleBaseUrlCandidates,
|
||||
appendPathToBaseUrl,
|
||||
buildAiProviderAuthHeaders,
|
||||
extractAiErrorMessage,
|
||||
};
|
||||
Reference in new issue
Block a user