feat: support configurable and discovered models in provider key tests
This commit is contained in:
parent
26341533ed
commit
9105599c64
2 changed files with 137 additions and 36 deletions
|
|
@ -84,7 +84,15 @@ export function renderProvidersPage(): string {
|
||||||
<div id="provider-test-area" style="display: none; border-top: 1px solid var(--border-dark); padding-top: 1rem;">
|
<div id="provider-test-area" style="display: none; border-top: 1px solid var(--border-dark); padding-top: 1rem;">
|
||||||
<div class="form-label">
|
<div class="form-label">
|
||||||
<span>Verify Keys Live</span>
|
<span>Verify Keys Live</span>
|
||||||
<span class="form-hint">Send a quick ping to test credentials</span>
|
<span class="form-hint">Tests configured model or /models discovery</span>
|
||||||
|
</div>
|
||||||
|
<div class="form-group" style="margin-top: 0.5rem; margin-bottom: 0.75rem;">
|
||||||
|
<label class="form-label" for="provider-test-model" style="font-size: 0.8rem;">
|
||||||
|
<span>Test Model (Optional)</span>
|
||||||
|
<span class="form-hint">Leave blank on Custom to check /models</span>
|
||||||
|
</label>
|
||||||
|
<input type="text" id="provider-test-model" class="form-input" list="provider-test-model-suggestions" placeholder="e.g. meta/llama-3.1-8b-instruct or gemini-2.5-flash" style="font-size: 0.85rem; padding: 0.45rem 0.65rem;">
|
||||||
|
<datalist id="provider-test-model-suggestions"></datalist>
|
||||||
</div>
|
</div>
|
||||||
<div id="provider-test-keys-list" style="display: flex; flex-direction: column; gap: 0.5rem; margin-top: 0.5rem;"></div>
|
<div id="provider-test-keys-list" style="display: flex; flex-direction: column; gap: 0.5rem; margin-top: 0.5rem;"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -215,14 +223,31 @@ export function renderProvidersPage(): string {
|
||||||
|
|
||||||
const testArea = document.getElementById('provider-test-area');
|
const testArea = document.getElementById('provider-test-area');
|
||||||
const testList = document.getElementById('provider-test-keys-list');
|
const testList = document.getElementById('provider-test-keys-list');
|
||||||
|
const testModelInput = document.getElementById('provider-test-model');
|
||||||
|
const testModelSuggestions = document.getElementById('provider-test-model-suggestions');
|
||||||
testList.innerHTML = '';
|
testList.innerHTML = '';
|
||||||
testArea.style.display = 'block';
|
testArea.style.display = 'block';
|
||||||
|
|
||||||
|
const configuredModels = Array.from(new Set(
|
||||||
|
(currentConfig.customModels || [])
|
||||||
|
.flatMap(m => m.tiers || [])
|
||||||
|
.flatMap(t => t.targets || [])
|
||||||
|
.filter(target => target.providerId === p.id && target.model && target.model.trim())
|
||||||
|
.map(target => target.model.trim())
|
||||||
|
));
|
||||||
|
const populateSuggestions = (models) => {
|
||||||
|
testModelSuggestions.innerHTML = models
|
||||||
|
.map(m => '<option value="' + escapeHtml(m) + '"></option>')
|
||||||
|
.join('');
|
||||||
|
};
|
||||||
|
populateSuggestions(configuredModels);
|
||||||
|
testModelInput.value = configuredModels[0] || '';
|
||||||
|
|
||||||
(p.keys || []).forEach((k, idx) => {
|
(p.keys || []).forEach((k, idx) => {
|
||||||
const row = document.createElement('div');
|
const row = document.createElement('div');
|
||||||
row.style.display = 'flex';
|
row.style.display = 'flex';
|
||||||
row.style.justifyContent = 'space-between';
|
row.style.flexDirection = 'column';
|
||||||
row.style.alignItems = 'center';
|
row.style.gap = '0.35rem';
|
||||||
row.style.background = 'rgba(0,0,0,0.2)';
|
row.style.background = 'rgba(0,0,0,0.2)';
|
||||||
row.style.padding = '0.5rem';
|
row.style.padding = '0.5rem';
|
||||||
row.style.borderRadius = '6px';
|
row.style.borderRadius = '6px';
|
||||||
|
|
@ -230,10 +255,13 @@ export function renderProvidersPage(): string {
|
||||||
|
|
||||||
const mask = k.key.length > 8 ? k.key.slice(0, 4) + '...' + k.key.slice(-4) : '****';
|
const mask = k.key.length > 8 ? k.key.slice(0, 4) + '...' + k.key.slice(-4) : '****';
|
||||||
row.innerHTML = \`
|
row.innerHTML = \`
|
||||||
<span><strong>Key \${idx + 1}:</strong> <code>\${escapeHtml(mask)}</code></span>
|
<div style="display: flex; justify-content: space-between; align-items: center;">
|
||||||
<button type="button" class="nav-btn test-single-key-btn" data-key="\${escapeHtml(k.key)}" style="padding: 3px 8px; font-size: 0.75rem;">
|
<span><strong>Key \${idx + 1}:</strong> <code>\${escapeHtml(mask)}</code></span>
|
||||||
<i class="fas fa-bolt"></i> Test
|
<button type="button" class="nav-btn test-single-key-btn" data-key="\${escapeHtml(k.key)}" style="padding: 3px 8px; font-size: 0.75rem;">
|
||||||
</button>
|
<i class="fas fa-bolt"></i> Test
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="test-key-result" style="display: none; font-size: 0.75rem; word-break: break-word;"></div>
|
||||||
\`;
|
\`;
|
||||||
testList.appendChild(row);
|
testList.appendChild(row);
|
||||||
});
|
});
|
||||||
|
|
@ -241,14 +269,24 @@ export function renderProvidersPage(): string {
|
||||||
testList.querySelectorAll('.test-single-key-btn').forEach(btn => {
|
testList.querySelectorAll('.test-single-key-btn').forEach(btn => {
|
||||||
btn.addEventListener('click', async () => {
|
btn.addEventListener('click', async () => {
|
||||||
const keyToTest = btn.getAttribute('data-key');
|
const keyToTest = btn.getAttribute('data-key');
|
||||||
|
const modelOverride = testModelInput.value.trim();
|
||||||
|
const resultEl = btn.closest('div').parentElement.querySelector('.test-key-result');
|
||||||
btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
|
btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
|
||||||
btn.disabled = true;
|
btn.disabled = true;
|
||||||
try {
|
try {
|
||||||
const res = await apiRequest('/api/admin/providers/' + p.id + '/test-key', {
|
const res = await apiRequest('/api/admin/providers/' + p.id + '/test-key', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: { key: keyToTest }
|
body: { key: keyToTest, modelOverride: modelOverride || undefined }
|
||||||
});
|
});
|
||||||
const result = await res.json();
|
const result = await res.json();
|
||||||
|
if (Array.isArray(result.models) && result.models.length > 0) {
|
||||||
|
populateSuggestions(Array.from(new Set([...configuredModels, ...result.models])));
|
||||||
|
}
|
||||||
|
if (resultEl) {
|
||||||
|
resultEl.style.display = 'block';
|
||||||
|
resultEl.style.color = result.success ? '#4ade80' : '#f87171';
|
||||||
|
resultEl.textContent = result.message;
|
||||||
|
}
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
btn.className = 'nav-btn primary';
|
btn.className = 'nav-btn primary';
|
||||||
btn.innerHTML = '<i class="fas fa-check"></i> ' + result.latencyMs + 'ms';
|
btn.innerHTML = '<i class="fas fa-check"></i> ' + result.latencyMs + 'ms';
|
||||||
|
|
@ -259,6 +297,11 @@ export function renderProvidersPage(): string {
|
||||||
showToast(result.message, 'error');
|
showToast(result.message, 'error');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
if (resultEl) {
|
||||||
|
resultEl.style.display = 'block';
|
||||||
|
resultEl.style.color = '#f87171';
|
||||||
|
resultEl.textContent = 'Test failed: ' + err.message;
|
||||||
|
}
|
||||||
btn.className = 'nav-btn danger';
|
btn.className = 'nav-btn danger';
|
||||||
btn.innerHTML = '<i class="fas fa-times"></i>';
|
btn.innerHTML = '<i class="fas fa-times"></i>';
|
||||||
showToast('Test failed: ' + err.message, 'error');
|
showToast('Test failed: ' + err.message, 'error');
|
||||||
|
|
|
||||||
|
|
@ -1839,7 +1839,7 @@ export class RevolverEngine {
|
||||||
providerId: string;
|
providerId: string;
|
||||||
key: string;
|
key: string;
|
||||||
modelOverride?: string;
|
modelOverride?: string;
|
||||||
}): Promise<{ success: boolean; latencyMs: number; message: string }> {
|
}): Promise<{ success: boolean; latencyMs: number; message: string; models?: string[] }> {
|
||||||
const provider = this.store.getProviderById(params.providerId);
|
const provider = this.store.getProviderById(params.providerId);
|
||||||
if (!provider) {
|
if (!provider) {
|
||||||
throw new Error(`Provider not found: ${params.providerId}`);
|
throw new Error(`Provider not found: ${params.providerId}`);
|
||||||
|
|
@ -1847,62 +1847,105 @@ export class RevolverEngine {
|
||||||
|
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
const type = provider.type;
|
const type = provider.type;
|
||||||
const model = params.modelOverride || (type === 'gemini' ? 'gemini-1.5-flash-latest' : type === 'groq' ? 'llama-3.3-70b-versatile' : 'gpt-4o-mini');
|
const configuredModel = this.store
|
||||||
|
.getCustomModels()
|
||||||
|
.flatMap(m => m.tiers)
|
||||||
|
.flatMap(t => t.targets)
|
||||||
|
.find(target => target.providerId === provider.id && target.model?.trim())
|
||||||
|
?.model?.trim();
|
||||||
|
|
||||||
|
const defaultModel =
|
||||||
|
type === 'gemini'
|
||||||
|
? 'gemini-1.5-flash-latest'
|
||||||
|
: type === 'groq'
|
||||||
|
? 'llama-3.3-70b-versatile'
|
||||||
|
: type === 'openai'
|
||||||
|
? 'gpt-4o-mini'
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
const model = params.modelOverride?.trim() || configuredModel || defaultModel;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (type === 'gemini') {
|
if (type === 'gemini') {
|
||||||
const genAI = new GoogleGenerativeAI(params.key);
|
const genAI = new GoogleGenerativeAI(params.key);
|
||||||
const m = genAI.getGenerativeModel({ model });
|
const m = genAI.getGenerativeModel({ model: model! });
|
||||||
const res = await m.generateContent('ping');
|
const res = await m.generateContent('ping');
|
||||||
const text = res.response.text();
|
const text = res.response.text();
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
latencyMs: Date.now() - start,
|
latencyMs: Date.now() - start,
|
||||||
message: `Connected successfully! Response: "${text.trim().slice(0, 50)}"`
|
message: `Connected (${model})! Response: "${text.trim().slice(0, 50)}"`
|
||||||
};
|
};
|
||||||
} else {
|
}
|
||||||
const baseUrl = (provider.baseUrl || this.store.getDefaultBaseUrl(type)).replace(/\/+$/, '');
|
|
||||||
const res = await fetch(`${baseUrl}/chat/completions`, {
|
const baseUrl = (provider.baseUrl || this.store.getDefaultBaseUrl(type)).replace(/\/+$/, '');
|
||||||
method: 'POST',
|
|
||||||
|
if (!model) {
|
||||||
|
const res = await fetch(`${baseUrl}/models`, {
|
||||||
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': `Bearer ${params.key}`,
|
'Authorization': `Bearer ${params.key}`,
|
||||||
'Accept-Encoding': 'identity'
|
'Accept-Encoding': 'identity'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
signal: AbortSignal.timeout(config.candidateTimeoutMs)
|
||||||
model,
|
|
||||||
messages: [{ role: 'user', content: 'ping' }],
|
|
||||||
max_tokens: 5
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
let errBody = '';
|
const errText = await this.readProviderErrorText(res);
|
||||||
try {
|
|
||||||
errBody = await res.text();
|
|
||||||
} catch (readErr: any) {
|
|
||||||
errBody = readErr.message || 'Failed to read error response';
|
|
||||||
}
|
|
||||||
let errText = errBody;
|
|
||||||
try {
|
|
||||||
const parsed = JSON.parse(errBody);
|
|
||||||
errText = parsed.error?.message || errBody;
|
|
||||||
} catch {}
|
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
latencyMs: Date.now() - start,
|
latencyMs: Date.now() - start,
|
||||||
message: `API returned ${res.status}: ${errText}`
|
message: `GET /models returned ${res.status}: ${errText} (enter a Test Model to test /chat/completions directly)`
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = (await res.json()) as any;
|
const data = (await res.json()) as any;
|
||||||
const text = data.choices?.[0]?.message?.content || 'OK';
|
const models: string[] = Array.isArray(data?.data)
|
||||||
|
? data.data.map((m: any) => m?.id).filter((id: any): id is string => typeof id === 'string')
|
||||||
|
: [];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
latencyMs: Date.now() - start,
|
latencyMs: Date.now() - start,
|
||||||
message: `Connected successfully! Response: "${text.trim().slice(0, 50)}"`
|
models: models.slice(0, 50),
|
||||||
|
message:
|
||||||
|
models.length > 0
|
||||||
|
? `Auth OK (${models.length} models found, e.g. ${models.slice(0, 3).join(', ')}). Pick or enter a Test Model to verify chat completions.`
|
||||||
|
: 'Auth OK via /models (empty model list). Enter a Test Model to verify chat completions.'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const res = await fetch(`${baseUrl}/chat/completions`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${params.key}`,
|
||||||
|
'Accept-Encoding': 'identity'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model,
|
||||||
|
messages: [{ role: 'user', content: 'ping' }],
|
||||||
|
max_tokens: 5
|
||||||
|
}),
|
||||||
|
signal: AbortSignal.timeout(config.candidateTimeoutMs)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const errText = await this.readProviderErrorText(res);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
latencyMs: Date.now() - start,
|
||||||
|
message: `API returned ${res.status} for model "${model}": ${errText}`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = (await res.json()) as any;
|
||||||
|
const text = data.choices?.[0]?.message?.content || 'OK';
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
latencyMs: Date.now() - start,
|
||||||
|
message: `Connected (${model})! Response: "${text.trim().slice(0, 50)}"`
|
||||||
|
};
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
const detailedMsg = err.cause?.message ? `${err.message}: ${err.cause.message}` : (err.message || 'Connection failed');
|
const detailedMsg = err.cause?.message ? `${err.message}: ${err.cause.message}` : (err.message || 'Connection failed');
|
||||||
return {
|
return {
|
||||||
|
|
@ -1912,6 +1955,21 @@ export class RevolverEngine {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async readProviderErrorText(res: globalThis.Response): Promise<string> {
|
||||||
|
let errBody = '';
|
||||||
|
try {
|
||||||
|
errBody = await res.text();
|
||||||
|
} catch (readErr: any) {
|
||||||
|
errBody = readErr.message || 'Failed to read error response';
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(errBody);
|
||||||
|
return parsed.error?.message || errBody;
|
||||||
|
} catch {
|
||||||
|
return errBody;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const revolverEngine = new RevolverEngine();
|
export const revolverEngine = new RevolverEngine();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue