Spaces:
Running
Running
Update js/iaConfigModule.js
Browse files- js/iaConfigModule.js +225 -248
js/iaConfigModule.js
CHANGED
|
@@ -1,277 +1,254 @@
|
|
| 1 |
-
//
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
* - Proveedores soportados: OpenAI, DeepSeek
|
| 8 |
-
* - Modelos OpenAI incluyen GPT‑5 / GPT‑5 mini y 4o / 4o‑mini
|
| 9 |
-
*/
|
| 10 |
-
|
| 11 |
-
export const llmProviders = [
|
| 12 |
-
{
|
| 13 |
-
name: "OpenAI",
|
| 14 |
-
value: "openai",
|
| 15 |
-
models: ["gpt-5", "gpt-5-mini", "gpt-4o", "gpt-4o-mini"],
|
| 16 |
-
url: "https://api.openai.com"
|
| 17 |
},
|
| 18 |
-
{
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
models:
|
| 22 |
-
url: "https://api.deepseek.com"
|
| 23 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
];
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
try {
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
return normalizeConfig(parsed);
|
| 34 |
-
} catch {
|
| 35 |
-
return defaultConfig();
|
| 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 |
-
* del payload (no convierte `input`↔`messages`).
|
| 76 |
-
*/
|
| 77 |
-
export function prepareLlmApiParams(userPayload = {}) {
|
| 78 |
-
const cfg = loadConfig();
|
| 79 |
-
const model = cfg?.llm?.model || "gpt-4o";
|
| 80 |
-
return { model, ...userPayload };
|
| 81 |
}
|
| 82 |
|
| 83 |
-
/**
|
| 84 |
-
* Renderiza un formulario simple para seleccionar proveedor/modelo/apiKey.
|
| 85 |
-
* containerId debe ser un <div id="..."> existente.
|
| 86 |
-
*/
|
| 87 |
export function renderIaConfigForm(containerId) {
|
| 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 |
</div>
|
|
|
|
| 117 |
</form>
|
| 118 |
`;
|
| 119 |
-
|
| 120 |
-
const
|
| 121 |
-
const
|
| 122 |
-
const
|
| 123 |
-
const
|
| 124 |
-
const
|
| 125 |
-
const
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
}
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
form.addEventListener('submit', (e) => {
|
| 147 |
e.preventDefault();
|
| 148 |
-
const
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
};
|
| 156 |
-
saveIaConfig(newCfg);
|
| 157 |
-
console.info('[iaConfig] guardado', newCfg);
|
| 158 |
-
alert('Configuración guardada');
|
| 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 |
-
/** Devuelve endpoint + payloadKind según proveedor/modelo */
|
| 187 |
-
export function endpointFor(provider, model) {
|
| 188 |
-
if (provider === 'openai') {
|
| 189 |
-
const isGpt5 = /^gpt-5/.test(model || '');
|
| 190 |
-
return { endpoint: isGpt5 ? '/v1/responses' : '/v1/chat/completions', payloadKind: isGpt5 ? 'responses' : 'chat' };
|
| 191 |
-
}
|
| 192 |
-
if (provider === 'deepseek') {
|
| 193 |
-
return { endpoint: '/v1/chat/completions', payloadKind: 'chat' };
|
| 194 |
-
}
|
| 195 |
-
return { endpoint: '/v1/chat/completions', payloadKind: 'chat' };
|
| 196 |
-
}
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
// ==============================
|
| 200 |
-
// File: js/analysisModule.js
|
| 201 |
-
// ==============================
|
| 202 |
-
|
| 203 |
-
import { getIaConfig, prepareLlmApiParams, endpointFor } from "./iaConfigModule.js";
|
| 204 |
-
import { callLLM } from "./llmClient.js";
|
| 205 |
-
|
| 206 |
-
/**
|
| 207 |
-
* Devuelve true si el modelo suele aceptar temperature != 1
|
| 208 |
-
* (evitamos razonadores / realtime / thinking, etc.)
|
| 209 |
-
*/
|
| 210 |
-
export function allowVariableTemperature(model) {
|
| 211 |
-
if (!model) return true;
|
| 212 |
-
// Evita familias que ignoran/rechazan temperature
|
| 213 |
-
return !/(?:\bthinking|\breason|\bo\d|\brealtime|\btranscribe)/i.test(model);
|
| 214 |
-
}
|
| 215 |
-
|
| 216 |
-
/**
|
| 217 |
-
* Analiza un texto transcrito (resumen clínico, flags, etc.) con el LLM actual.
|
| 218 |
-
* Devuelve el texto del LLM o lanza error.
|
| 219 |
-
*/
|
| 220 |
-
export async function analyzeMedical(transcript) {
|
| 221 |
-
if (!transcript || !transcript.trim()) {
|
| 222 |
-
throw new Error("Texto vacío para análisis.");
|
| 223 |
-
}
|
| 224 |
-
|
| 225 |
-
const config = getIaConfig();
|
| 226 |
-
const provider = config?.llm?.provider || 'openai';
|
| 227 |
-
const model = config?.llm?.model || '';
|
| 228 |
-
const baseUrl = (config?.llm?.baseUrl || '').replace(/\/$/, '');
|
| 229 |
-
const apiKey = config?.llm?.apiKey || '';
|
| 230 |
-
|
| 231 |
-
const { endpoint, payloadKind } = endpointFor(provider, model);
|
| 232 |
-
|
| 233 |
-
// Mensajes
|
| 234 |
-
const messages = [
|
| 235 |
-
{ role: "system", content: "Eres un médico de urgencias preciso y claro. Resume con puntos clave y un plan." },
|
| 236 |
-
{ role: "user", content: `Analiza la siguiente transcripción clínica y devuelve un informe breve y útil.\n\nTranscripción:\n${transcript}` }
|
| 237 |
-
];
|
| 238 |
-
|
| 239 |
-
// Parámetros comunes: temperature solo si aplica
|
| 240 |
-
const useTemp = allowVariableTemperature(model);
|
| 241 |
-
const temperature = useTemp ? 0.3 : undefined;
|
| 242 |
-
|
| 243 |
-
// Construye payload según endpoint
|
| 244 |
-
let payload;
|
| 245 |
-
if (provider === 'openai' && payloadKind === 'responses') {
|
| 246 |
-
// Responses API (GPT‑5, GPT‑5 mini)
|
| 247 |
-
payload = prepareLlmApiParams({
|
| 248 |
-
model,
|
| 249 |
-
input: messages,
|
| 250 |
-
temperature
|
| 251 |
-
// puedes añadir max_output_tokens, top_p, etc.
|
| 252 |
-
});
|
| 253 |
-
} else {
|
| 254 |
-
// Chat Completions (OpenAI 4o/mini, DeepSeek, etc.)
|
| 255 |
-
payload = prepareLlmApiParams({
|
| 256 |
-
model,
|
| 257 |
-
messages,
|
| 258 |
-
temperature
|
| 259 |
-
});
|
| 260 |
-
}
|
| 261 |
-
|
| 262 |
-
// Llamada: delegamos en llmClient para HTTP real
|
| 263 |
-
const { text } = await callLLM(messages, {
|
| 264 |
-
provider,
|
| 265 |
-
baseUrl,
|
| 266 |
-
apiKey,
|
| 267 |
-
endpoint,
|
| 268 |
-
model,
|
| 269 |
-
payloadKind,
|
| 270 |
-
payload
|
| 271 |
-
});
|
| 272 |
-
|
| 273 |
-
if (!text) {
|
| 274 |
-
throw new Error('La IA no devolvió texto.');
|
| 275 |
-
}
|
| 276 |
-
return text;
|
| 277 |
-
}
|
|
|
|
| 1 |
+
// js/iaConfigModule.js
|
| 2 |
+
const defaultConfig = {
|
| 3 |
+
llm: {
|
| 4 |
+
provider: "deepseek",
|
| 5 |
+
apiKeys: { deepseek: "", openai: "" },
|
| 6 |
+
model: "deepseek-chat"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
},
|
| 8 |
+
transcription: {
|
| 9 |
+
provider: "openai",
|
| 10 |
+
apiKeys: { openai: "", deepgram: "" },
|
| 11 |
+
models: { openai: "whisper-1", deepgram: "nova-2" }
|
|
|
|
| 12 |
}
|
| 13 |
+
};
|
| 14 |
+
// CORRECCIÓN: Nombres de modelos de OpenAI actualizados a versiones canónicas y estables.
|
| 15 |
+
export const llmProviders = [
|
| 16 |
+
{ name: "OpenAI", value: "openai", models: ["gpt-4o", "gpt-4o-mini"], url: "https://api.openai.com" },
|
| 17 |
+
{ name: "DeepSeek", value: "deepseek", models: ["deepseek-chat", "deepseek-reasoner"], url: "https://api.deepseek.com" }
|
| 18 |
];
|
| 19 |
+
export const transcriptionProviders = [
|
| 20 |
+
{ name: "OpenAI Whisper", value: "openai", models: ["whisper-1"], url: "https://api.openai.com" },
|
| 21 |
+
{ name: "Deepgram", value: "deepgram", models: ["nova-2", "whisper-large"], url: "https://api.deepgram.com" }
|
| 22 |
+
];
|
| 23 |
+
function saveConfig(config) {
|
| 24 |
try {
|
| 25 |
+
localStorage.setItem("iaConfig", JSON.stringify(config));
|
| 26 |
+
} catch (e) {
|
| 27 |
+
console.error("[iaConfigModule] Error guardando la configuración:", e);
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
+
function clone(obj) {
|
| 32 |
+
// structuredClone es moderno y robusto, con un fallback seguro.
|
| 33 |
+
try {
|
| 34 |
+
return structuredClone(obj);
|
| 35 |
+
} catch (e) {
|
| 36 |
+
return JSON.parse(JSON.stringify(obj));
|
| 37 |
+
}
|
| 38 |
}
|
| 39 |
|
| 40 |
+
function loadConfig() {
|
| 41 |
+
let config;
|
| 42 |
+
try {
|
| 43 |
+
const storedConfig = localStorage.getItem("iaConfig");
|
| 44 |
+
config = storedConfig ?
|
| 45 |
+
JSON.parse(storedConfig) : clone(defaultConfig);
|
| 46 |
+
} catch (error) {
|
| 47 |
+
console.error("[iaConfigModule] Error al parsear config, usando defaults.", error);
|
| 48 |
+
config = clone(defaultConfig);
|
| 49 |
+
}
|
| 50 |
|
| 51 |
+
let needsSave = false;
|
| 52 |
+
|
| 53 |
+
// CORRECCIÓN: La lógica de migración para 'transcription' era defectuosa.
|
| 54 |
+
// Podía borrar la API key de Deepgram si era el proveedor antiguo.
|
| 55 |
+
if (config.transcription.apiKey !== undefined) {
|
| 56 |
+
console.log("[iaConfigModule] Migrando config antigua de transcripción...");
|
| 57 |
+
const oldKey = config.transcription.apiKey;
|
| 58 |
+
const oldModel = config.transcription.model;
|
| 59 |
+
const oldProvider = config.transcription.provider;
|
| 60 |
+
// Se usa el spread operator para no perder las keys de otros proveedores.
|
| 61 |
+
config.transcription.apiKeys = { ...defaultConfig.transcription.apiKeys, [oldProvider]: oldKey };
|
| 62 |
+
config.transcription.models = { ...defaultConfig.transcription.models, [oldProvider]: oldModel };
|
| 63 |
+
delete config.transcription.apiKey;
|
| 64 |
+
delete config.transcription.model;
|
| 65 |
+
needsSave = true;
|
| 66 |
+
}
|
| 67 |
|
| 68 |
+
if (config.llm.apiKey !== undefined) {
|
| 69 |
+
console.log("[iaConfigModule] Migrando config antigua de LLM...");
|
| 70 |
+
const old = config.llm.apiKey;
|
| 71 |
+
config.llm.apiKeys = { ...defaultConfig.llm.apiKeys, [config.llm.provider]: old };
|
| 72 |
+
delete config.llm.apiKey;
|
| 73 |
+
needsSave = true;
|
| 74 |
+
}
|
| 75 |
|
| 76 |
+
if (config.llm.provider === 'deepseek' && (config.llm.model === 'deepseek-v3' || config.llm.model === 'deepseek-llm')) {
|
| 77 |
+
config.llm.model = 'deepseek-chat';
|
| 78 |
+
needsSave = true;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
if (needsSave) {
|
| 82 |
+
saveConfig(config);
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
return config;
|
| 86 |
}
|
| 87 |
|
| 88 |
+
export function getIaConfig() {
|
| 89 |
+
return loadConfig();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
export function renderIaConfigForm(containerId) {
|
| 93 |
+
let config = loadConfig();
|
| 94 |
+
const container = document.getElementById(containerId);
|
| 95 |
+
// CORRECCIÓN: Errores de sintaxis en el manejo de errores.
|
| 96 |
+
if (!container) {
|
| 97 |
+
console.error(`[iaConfigModule] No se encontró el contenedor '${containerId}'`);
|
| 98 |
+
document.body.insertAdjacentHTML('beforeend', `<div style='color:red; padding: 1rem;'>[Error] No se encontró el contenedor '${containerId}' para la configuración IA.</div>`);
|
| 99 |
+
return;
|
| 100 |
+
}
|
| 101 |
|
| 102 |
+
function maskApiKey(key) {
|
| 103 |
+
if (!key) return '';
|
| 104 |
+
if (key.length <= 8) return '*'.repeat(key.length);
|
| 105 |
+
return `${key.substring(0, 3)}-****-${key.slice(-4)}`;
|
| 106 |
+
}
|
| 107 |
|
| 108 |
+
// CORRECCIÓN: Errores de sintaxis en la plantilla HTML (faltaban backticks ``).
|
| 109 |
+
container.innerHTML = `
|
| 110 |
+
<div class="flex justify-between items-center mb-6 border-b pb-2 border-blue-100">
|
| 111 |
+
<h2 class="text-xl font-bold text-blue-700 flex items-center"><i class='fas fa-cogs mr-2'></i>Configurar Proveedores IA</h2>
|
| 112 |
+
<button id="btnCloseConfig" type="button" class="text-gray-500 hover:text-blue-600 text-2xl focus:outline-none" aria-label="Cerrar"><i class="fas fa-times"></i></button>
|
| 113 |
+
</div>
|
| 114 |
+
<form id="iaConfigForm" class="space-y-6">
|
| 115 |
+
<div class="bg-blue-50 p-4 rounded-lg border border-blue-100">
|
| 116 |
+
<label class="block font-semibold text-blue-800 mb-2">Proveedor LLM</label>
|
| 117 |
+
<select id="llmProvider" class="w-full mb-3 p-2 rounded border border-gray-300 focus:ring-2 focus:ring-blue-300">${llmProviders.map(p => `<option value="${p.value}">${p.name}</option>`).join("")}</select>
|
| 118 |
+
|
| 119 |
+
<div class="flex items-center mb-3">
|
| 120 |
+
<input type="password" id="llmApiKey" class="flex-1 p-2 rounded border border-gray-300 mr-2 bg-gray-100" placeholder="API Key LLM" autocomplete="off">
|
| 121 |
+
<button class="text-blue-700 hover:text-blue-900 px-3 py-2 rounded focus:outline-none border border-blue-200 bg-white" type="button" id="toggleLlmApiKey"><i class="fas fa-eye"></i></button>
|
| 122 |
+
</div>
|
| 123 |
+
<select id="llmModel" class="w-full p-2 rounded border border-gray-300 focus:ring-2 focus:ring-blue-300"></select>
|
| 124 |
+
</div>
|
| 125 |
+
<div class="bg-purple-50 p-4 rounded-lg border border-purple-100">
|
| 126 |
+
|
| 127 |
+
<label class="block font-semibold text-purple-800 mb-2">Proveedor Transcripción</label>
|
| 128 |
+
<select id="transProvider" class="w-full mb-3 p-2 rounded border border-gray-300 focus:ring-2 focus:ring-purple-300">${transcriptionProviders.map(p => `<option value="${p.value}">${p.name}</option>`).join("")}</select>
|
| 129 |
+
<div class="flex items-center mb-3">
|
| 130 |
+
<input type="password" id="transApiKey" class="flex-1 p-2 rounded border border-gray-300 mr-2 bg-gray-100" placeholder="API Key Transcripción" autocomplete="off">
|
| 131 |
+
<button class="text-purple-700 hover:text-purple-900 px-3 py-2 rounded focus:outline-none border border-purple-200 bg-white" type="button" id="toggleTransApiKey"><i class="fas fa-eye"></i></button>
|
| 132 |
+
</div>
|
| 133 |
+
|
| 134 |
+
<select id="transModel" class="w-full p-2 rounded border border-gray-300 focus:ring-2 focus:ring-purple-300"></select>
|
| 135 |
</div>
|
| 136 |
+
<button type="submit" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-semibold py-3 rounded-lg shadow transition-colors flex items-center justify-center text-lg"><i class="fas fa-save mr-2"></i>Guardar configuración</button>
|
| 137 |
</form>
|
| 138 |
`;
|
| 139 |
+
// Elementos del DOM
|
| 140 |
+
const llmProviderSelect = document.getElementById("llmProvider");
|
| 141 |
+
const llmModelSelect = document.getElementById("llmModel");
|
| 142 |
+
const llmApiKeyInput = document.getElementById("llmApiKey");
|
| 143 |
+
const transProviderSelect = document.getElementById("transProvider");
|
| 144 |
+
const transModelSelect = document.getElementById("transModel");
|
| 145 |
+
const transApiKeyInput = document.getElementById("transApiKey");
|
| 146 |
+
|
| 147 |
+
// Listeners
|
| 148 |
+
document.getElementById("btnCloseConfig")?.addEventListener("click", () => document.getElementById("configModal")?.classList.remove("active"));
|
| 149 |
+
document.getElementById("toggleLlmApiKey").addEventListener("click", () => { llmApiKeyInput.type = llmApiKeyInput.type === "password" ? "text" : "password"; });
|
| 150 |
+
document.getElementById("toggleTransApiKey").addEventListener("click", () => { transApiKeyInput.type = transApiKeyInput.type === "password" ? "text" : "password"; });
|
| 151 |
+
|
| 152 |
+
// ===================== INICIO DE LA CORRECCIÓN =====================
|
| 153 |
+
// CORRECCIÓN: Lógica de actualización de UI refactorizada para ser más clara y correcta.
|
| 154 |
+
function updateLlmUI() {
|
| 155 |
+
const selectedProvider = llmProviderSelect.value;
|
| 156 |
+
const providerObj = llmProviders.find(p => p.value === selectedProvider);
|
| 157 |
+
if (!providerObj) return;
|
| 158 |
+
|
| 159 |
+
// 1. Popular siempre la lista de modelos del proveedor seleccionado
|
| 160 |
+
llmModelSelect.innerHTML = providerObj.models.map(m => `<option value="${m}">${m}</option>`).join("");
|
| 161 |
+
|
| 162 |
+
// 2. Decidir qué modelo seleccionar
|
| 163 |
+
let modelToSelect = providerObj.models[0]; // Por defecto, el primero de la lista
|
| 164 |
+
// Si el proveedor seleccionado es el que ya está guardado en la config...
|
| 165 |
+
if (selectedProvider === config.llm.provider) {
|
| 166 |
+
// ...y si el modelo guardado es válido para este proveedor...
|
| 167 |
+
if (providerObj.models.includes(config.llm.model)) {
|
| 168 |
+
// ...entonces usamos el modelo guardado.
|
| 169 |
+
modelToSelect = config.llm.model;
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
llmModelSelect.value = modelToSelect;
|
| 173 |
+
|
| 174 |
+
// 3. Actualizar la API Key
|
| 175 |
+
llmApiKeyInput.value = maskApiKey(config.llm.apiKeys[selectedProvider] || '');
|
| 176 |
+
}
|
| 177 |
+
// ===================== FIN DE LA CORRECCIÓN =======================
|
| 178 |
+
|
| 179 |
+
function updateTransUI() {
|
| 180 |
+
const selectedProvider = transProviderSelect.value;
|
| 181 |
+
const providerObj = transcriptionProviders.find(p => p.value === selectedProvider);
|
| 182 |
+
if (!providerObj) return;
|
| 183 |
+
|
| 184 |
+
transModelSelect.innerHTML = providerObj.models.map(m => `<option value="${m}">${m}</option>`).join("");
|
| 185 |
+
// La estructura de 'models' en transcripción permite una búsqueda directa.
|
| 186 |
+
transModelSelect.value = config.transcription.models[selectedProvider] || providerObj.models[0];
|
| 187 |
+
transApiKeyInput.value = maskApiKey(config.transcription.apiKeys[selectedProvider] || '');
|
| 188 |
}
|
| 189 |
|
| 190 |
+
llmProviderSelect.addEventListener("change", updateLlmUI);
|
| 191 |
+
transProviderSelect.addEventListener("change", updateTransUI);
|
| 192 |
+
|
| 193 |
+
// Estado inicial del formulario
|
| 194 |
+
llmProviderSelect.value = config.llm.provider;
|
| 195 |
+
transProviderSelect.value = config.transcription.provider;
|
| 196 |
+
updateLlmUI();
|
| 197 |
+
updateTransUI();
|
| 198 |
+
|
| 199 |
+
// Guardar configuración
|
| 200 |
+
document.getElementById("iaConfigForm").addEventListener("submit", e => {
|
|
|
|
|
|
|
| 201 |
e.preventDefault();
|
| 202 |
+
const prevConfig = config;
|
| 203 |
+
const newConfig = clone(prevConfig);
|
| 204 |
+
|
| 205 |
+
// Guardado de LLM
|
| 206 |
+
const llmProv = llmProviderSelect.value;
|
| 207 |
+
const rawLlmKey = llmApiKeyInput.value;
|
| 208 |
+
const oldLlmKey = prevConfig.llm.apiKeys[llmProv] || '';
|
| 209 |
+
const actualLlmKey = rawLlmKey === maskApiKey(oldLlmKey) ? oldLlmKey : rawLlmKey;
|
| 210 |
+
newConfig.llm = {
|
| 211 |
+
...prevConfig.llm,
|
| 212 |
+
provider: llmProv,
|
| 213 |
+
model: llmModelSelect.value,
|
| 214 |
+
apiKeys: { ...prevConfig.llm.apiKeys, [llmProv]: actualLlmKey }
|
| 215 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
+
// Guardado de Transcripción
|
| 218 |
+
const transProv = transProviderSelect.value;
|
| 219 |
+
const rawTransKey = transApiKeyInput.value;
|
| 220 |
+
const oldTransKey = prevConfig.transcription.apiKeys[transProv] || '';
|
| 221 |
+
const actualTransKey = rawTransKey === maskApiKey(oldTransKey) ? oldTransKey : rawTransKey;
|
| 222 |
+
newConfig.transcription = {
|
| 223 |
+
...prevConfig.transcription,
|
| 224 |
+
provider: transProv,
|
| 225 |
+
models: { ...prevConfig.transcription.models, [transProv]: transModelSelect.value },
|
| 226 |
+
apiKeys: { ...prevConfig.transcription.apiKeys, [transProv]: actualTransKey }
|
| 227 |
+
};
|
| 228 |
+
|
| 229 |
+
saveConfig(newConfig);
|
| 230 |
+
config = newConfig; // Actualizar la config en memoria.
|
| 231 |
+
|
| 232 |
+
document.dispatchEvent(new CustomEvent('iaConfigChanged'));
|
| 233 |
+
|
| 234 |
+
// Resetear UI post-guardado
|
| 235 |
+
llmApiKeyInput.value = maskApiKey(newConfig.llm.apiKeys[newConfig.llm.provider] || '');
|
| 236 |
+
transApiKeyInput.value = maskApiKey(newConfig.transcription.apiKeys[newConfig.transcription.provider] || '');
|
| 237 |
+
llmApiKeyInput.type = "password";
|
| 238 |
+
transApiKeyInput.type = "password";
|
| 239 |
+
|
| 240 |
+
// Mensaje de éxito
|
| 241 |
+
let msg = document.getElementById('iaConfigSavedMsg');
|
| 242 |
+
if (!msg) {
|
| 243 |
+
msg = document.createElement('div');
|
| 244 |
+
msg.id = 'iaConfigSavedMsg';
|
| 245 |
+
msg.className = 'fixed left-1/2 top-6 -translate-x-1/2 bg-green-500 text-white px-6 py-3 rounded shadow-lg text-lg z-50';
|
| 246 |
+
msg.innerHTML = '<i class="fas fa-check-circle mr-2"></i>¡Configuración guardada!';
|
| 247 |
+
document.body.appendChild(msg);
|
| 248 |
}
|
| 249 |
+
msg.style.display = 'block';
|
| 250 |
+
setTimeout(() => { msg.style.display = 'none'; }, 2000);
|
| 251 |
+
|
| 252 |
+
document.getElementById("configModal")?.classList.remove("active");
|
| 253 |
});
|
| 254 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|