Remove fallback voices - fail properly if API doesn't respond
This commit is contained in:
parent
4e5ec776a8
commit
7a658f7bdf
1 changed files with 43 additions and 59 deletions
|
|
@ -363,68 +363,52 @@ async function openUncloseaiEmbeddedModalNew() {
|
|||
|
||||
// Load voices dynamically from API
|
||||
const loadVoices = async () => {
|
||||
try {
|
||||
const { VOICES_API_URL } = await import("./config.js");
|
||||
const response = await fetch(VOICES_API_URL);
|
||||
const { VOICES_API_URL } = await import("./config.js");
|
||||
const response = await fetch(VOICES_API_URL);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const voices = data.voices || [];
|
||||
|
||||
voiceSelect.innerHTML = ''; // Clear existing options
|
||||
|
||||
if (voices.length > 0) {
|
||||
loadedVoicesCount = voices.length;
|
||||
|
||||
// Group voices by model
|
||||
const groupedVoices = {};
|
||||
voices.forEach(v => {
|
||||
if (!groupedVoices[v.model]) {
|
||||
groupedVoices[v.model] = [];
|
||||
}
|
||||
groupedVoices[v.model].push(v.voice);
|
||||
});
|
||||
|
||||
// Add options grouped by model
|
||||
Object.keys(groupedVoices).sort().forEach(model => {
|
||||
const optgroup = document.createElement("optgroup");
|
||||
optgroup.label = model;
|
||||
groupedVoices[model].forEach(voice => {
|
||||
const option = document.createElement("option");
|
||||
option.value = `${model}:${voice}`; // Store model:voice format
|
||||
option.textContent = voice.charAt(0).toUpperCase() + voice.slice(1);
|
||||
optgroup.appendChild(option);
|
||||
});
|
||||
voiceSelect.appendChild(optgroup);
|
||||
});
|
||||
|
||||
console.log("Voices loaded:", voices.length, "voices from API");
|
||||
} else {
|
||||
// No voices from API, use fallback
|
||||
throw new Error("No voices in API response");
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Failed to load voices from API, using fallback:", error);
|
||||
// Fallback to default voices with model prefix
|
||||
const fallbackVoices = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"];
|
||||
loadedVoicesCount = fallbackVoices.length;
|
||||
voiceSelect.innerHTML = '';
|
||||
fallbackVoices.forEach((voice) => {
|
||||
const option = document.createElement("option");
|
||||
option.value = `tts-1:${voice}`; // Store model:voice format
|
||||
option.textContent = voice.charAt(0).toUpperCase() + voice.slice(1);
|
||||
voiceSelect.appendChild(option);
|
||||
});
|
||||
console.log("Voices loaded:", fallbackVoices.length, "fallback voices");
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
// Load saved voice from localStorage or default to tts-1:alloy
|
||||
const savedVoice = localStorage.getItem("selectedVoice") || "tts-1:alloy";
|
||||
voiceSelect.value = savedVoice;
|
||||
console.log("Selected voice:", savedVoice);
|
||||
const data = await response.json();
|
||||
const voices = data.voices || [];
|
||||
|
||||
if (voices.length === 0) {
|
||||
throw new Error("No voices in API response");
|
||||
}
|
||||
|
||||
voiceSelect.innerHTML = ''; // Clear existing options
|
||||
loadedVoicesCount = voices.length;
|
||||
|
||||
// Group voices by model
|
||||
const groupedVoices = {};
|
||||
voices.forEach(v => {
|
||||
if (!groupedVoices[v.model]) {
|
||||
groupedVoices[v.model] = [];
|
||||
}
|
||||
groupedVoices[v.model].push(v.voice);
|
||||
});
|
||||
|
||||
// Add options grouped by model
|
||||
Object.keys(groupedVoices).sort().forEach(model => {
|
||||
const optgroup = document.createElement("optgroup");
|
||||
optgroup.label = model;
|
||||
groupedVoices[model].forEach(voice => {
|
||||
const option = document.createElement("option");
|
||||
option.value = `${model}:${voice}`; // Store model:voice format
|
||||
option.textContent = voice.charAt(0).toUpperCase() + voice.slice(1);
|
||||
optgroup.appendChild(option);
|
||||
});
|
||||
voiceSelect.appendChild(optgroup);
|
||||
});
|
||||
|
||||
// Load saved voice from localStorage or default to first voice
|
||||
const savedVoice = localStorage.getItem("selectedVoice");
|
||||
if (savedVoice && voiceSelect.querySelector(`option[value="${savedVoice}"]`)) {
|
||||
voiceSelect.value = savedVoice;
|
||||
}
|
||||
|
||||
console.log("Voices loaded:", voices.length, "voices from API");
|
||||
};
|
||||
|
||||
// Save voice selection to localStorage when changed
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue