export const FALLBACK_MODELS = {
openai: ['whisper-1', 'gpt-4o-transcribe', 'gpt-4o-mini-transcribe'],
local: [
'Systran/faster-whisper-small',
'Systran/faster-whisper-small.en',
'Systran/faster-whisper-medium.en',
],
network: [
'Systran/faster-whisper-base.en',
'Systran/faster-whisper-small.en',
'Systran/faster-whisper-medium.en',
],
};
export function kindDefaults(kind) {
if (kind === 'local')
return { host: 'http://127.0.0.1', port: '5200', model: FALLBACK_MODELS.local[0] };
if (kind === 'openai')
return { host: 'https://api.openai.com', port: '443', model: 'whisper-1' };
return { host: '', port: '', model: FALLBACK_MODELS.network[0] };
}
export function normalizeHost(h) {
h = (h || '').trim().replace(/\/$/, '');
if (!h) return h;
if (!/^https?:\/\//i.test(h)) return 'http://' + h;
return h;
}
export function parseUrlIntoFields(raw) {
if (!raw) return null;
let normalised = raw.trim();
if (!/^https?:\/\//i.test(normalised)) normalised = 'http://' + normalised;
let u;
try {
u = new URL(normalised);
} catch (_) {
return null;
}
const host = u.protocol + '//' + u.hostname;
const port = u.port || (u.protocol === 'https:' ? '443' : '80');
return { host, port };
}
export async function fetchModels(kind, host, port) {
const query =
'?kind=' +
encodeURIComponent(kind) +
'&host=' +
encodeURIComponent(normalizeHost(host)) +
'&port=' +
encodeURIComponent(port || '');
try {
const resp = await fetch('/api/stt/models' + query);
if (!resp.ok) throw new Error('not ok');
const data = await resp.json();
if (!data.models || !data.models.length) throw new Error('empty');
return data.models;
} catch (_) {
return FALLBACK_MODELS[kind] || FALLBACK_MODELS.local;
}
}