Spaces:
Running
Running
Update js/iaConfigModule.js
Browse files- js/iaConfigModule.js +247 -224
js/iaConfigModule.js
CHANGED
|
@@ -1,254 +1,277 @@
|
|
| 1 |
-
//
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
},
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
models:
|
|
|
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
function saveConfig(config) {
|
| 24 |
try {
|
| 25 |
-
localStorage.
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
-
function
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
} catch (e) {
|
| 36 |
-
return JSON.parse(JSON.stringify(obj));
|
| 37 |
-
}
|
| 38 |
}
|
| 39 |
|
| 40 |
-
function
|
| 41 |
-
|
| 42 |
-
|
| 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 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 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 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
delete config.llm.apiKey;
|
| 73 |
-
needsSave = true;
|
| 74 |
-
}
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
saveConfig(config);
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
return config;
|
| 86 |
}
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
export function renderIaConfigForm(containerId) {
|
| 93 |
-
|
| 94 |
-
|
| 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 |
-
|
| 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 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
<
|
| 116 |
-
|
| 117 |
-
<
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
</
|
| 123 |
-
|
| 124 |
-
<
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
<
|
| 130 |
-
|
| 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 |
-
|
| 140 |
-
const
|
| 141 |
-
const
|
| 142 |
-
const
|
| 143 |
-
const
|
| 144 |
-
const
|
| 145 |
-
const
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 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 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
| 201 |
e.preventDefault();
|
| 202 |
-
const
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 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 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 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 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ==============================
|
| 2 |
+
// File: js/iaConfigModule.js
|
| 3 |
+
// ==============================
|
| 4 |
+
|
| 5 |
+
/**
|
| 6 |
+
* Gestión de configuración de IA en localStorage + UI.
|
| 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 |
+
name: "DeepSeek",
|
| 20 |
+
value: "deepseek",
|
| 21 |
+
models: ["deepseek-chat", "deepseek-reasoner"],
|
| 22 |
+
url: "https://api.deepseek.com"
|
| 23 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
];
|
| 25 |
+
|
| 26 |
+
const STORAGE_KEY = "iaConfig";
|
| 27 |
+
|
| 28 |
+
export function loadConfig() {
|
|
|
|
| 29 |
try {
|
| 30 |
+
const raw = localStorage.getItem(STORAGE_KEY);
|
| 31 |
+
if (!raw) return defaultConfig();
|
| 32 |
+
const parsed = JSON.parse(raw);
|
| 33 |
+
return normalizeConfig(parsed);
|
| 34 |
+
} catch {
|
| 35 |
+
return defaultConfig();
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
+
export function saveIaConfig(cfg) {
|
| 40 |
+
const normalized = normalizeConfig(cfg);
|
| 41 |
+
localStorage.setItem(STORAGE_KEY, JSON.stringify(normalized));
|
| 42 |
+
return normalized;
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
|
| 45 |
+
export function getIaConfig() {
|
| 46 |
+
return loadConfig();
|
| 47 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
function defaultConfig() {
|
| 50 |
+
return {
|
| 51 |
+
llm: {
|
| 52 |
+
provider: "openai",
|
| 53 |
+
model: "gpt-4o",
|
| 54 |
+
apiKey: "",
|
| 55 |
+
baseUrl: providerBaseUrl("openai")
|
| 56 |
+
}
|
| 57 |
+
};
|
| 58 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
function providerBaseUrl(provider) {
|
| 61 |
+
const p = llmProviders.find(p => p.value === provider);
|
| 62 |
+
return p?.url || "";
|
| 63 |
+
}
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
function normalizeConfig(cfg) {
|
| 66 |
+
const provider = cfg?.llm?.provider || "openai";
|
| 67 |
+
const baseUrl = cfg?.llm?.baseUrl || providerBaseUrl(provider);
|
| 68 |
+
const model = cfg?.llm?.model || (provider === "openai" ? "gpt-4o" : "deepseek-chat");
|
| 69 |
+
const apiKey = cfg?.llm?.apiKey || "";
|
| 70 |
+
return { llm: { provider, model, apiKey, baseUrl } };
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
+
/**
|
| 74 |
+
* Prepara parámetros por defecto sin cambiar el shape específico
|
| 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 |
+
const root = document.getElementById(containerId);
|
| 89 |
+
if (!root) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
const cfg = loadConfig();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
root.innerHTML = `
|
| 94 |
+
<form id="iaCfgForm" class="ia-form" style="display:grid;gap:.5rem;align-items:center;max-width:520px">
|
| 95 |
+
<label>
|
| 96 |
+
<span style="display:block;font-weight:600">Proveedor</span>
|
| 97 |
+
<select id="iaProvider" required>
|
| 98 |
+
${llmProviders.map(p => `<option value="${p.value}">${p.name}</option>`).join("")}
|
| 99 |
+
</select>
|
| 100 |
+
</label>
|
| 101 |
+
<label>
|
| 102 |
+
<span style="display:block;font-weight:600">Modelo</span>
|
| 103 |
+
<select id="iaModel" required></select>
|
| 104 |
+
</label>
|
| 105 |
+
<label>
|
| 106 |
+
<span style="display:block;font-weight:600">API Key</span>
|
| 107 |
+
<input id="iaApiKey" type="password" placeholder="sk-..." autocomplete="off" />
|
| 108 |
+
</label>
|
| 109 |
+
<label>
|
| 110 |
+
<span style="display:block;font-weight:600">Base URL</span>
|
| 111 |
+
<input id="iaBaseUrl" type="text" placeholder="https://api.openai.com" />
|
| 112 |
+
</label>
|
| 113 |
+
<div style="display:flex;gap:.5rem">
|
| 114 |
+
<button type="submit">Guardar</button>
|
| 115 |
+
<button type="button" id="iaTestBtn">Probar</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
</div>
|
|
|
|
| 117 |
</form>
|
| 118 |
`;
|
| 119 |
+
|
| 120 |
+
const selProvider = root.querySelector('#iaProvider');
|
| 121 |
+
const selModel = root.querySelector('#iaModel');
|
| 122 |
+
const inpApiKey = root.querySelector('#iaApiKey');
|
| 123 |
+
const inpBaseUrl = root.querySelector('#iaBaseUrl');
|
| 124 |
+
const form = root.querySelector('#iaCfgForm');
|
| 125 |
+
const btnTest = root.querySelector('#iaTestBtn');
|
| 126 |
+
|
| 127 |
+
function refreshModels() {
|
| 128 |
+
const provider = selProvider.value;
|
| 129 |
+
const def = llmProviders.find(p => p.value === provider);
|
| 130 |
+
const models = def?.models || [];
|
| 131 |
+
selModel.innerHTML = models.map(m => `<option value="${m}">${m}</option>`).join("");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
}
|
| 133 |
|
| 134 |
+
// Inicializa
|
| 135 |
+
selProvider.value = cfg.llm.provider;
|
| 136 |
+
refreshModels();
|
| 137 |
+
selModel.value = cfg.llm.model;
|
| 138 |
+
inpApiKey.value = cfg.llm.apiKey;
|
| 139 |
+
inpBaseUrl.value = cfg.llm.baseUrl || providerBaseUrl(selProvider.value);
|
| 140 |
+
|
| 141 |
+
selProvider.addEventListener('change', () => {
|
| 142 |
+
refreshModels();
|
| 143 |
+
inpBaseUrl.value = providerBaseUrl(selProvider.value);
|
| 144 |
+
});
|
| 145 |
+
|
| 146 |
+
form.addEventListener('submit', (e) => {
|
| 147 |
e.preventDefault();
|
| 148 |
+
const newCfg = {
|
| 149 |
+
llm: {
|
| 150 |
+
provider: selProvider.value,
|
| 151 |
+
model: selModel.value,
|
| 152 |
+
apiKey: inpApiKey.value.trim(),
|
| 153 |
+
baseUrl: inpBaseUrl.value.trim()
|
| 154 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
};
|
| 156 |
+
saveIaConfig(newCfg);
|
| 157 |
+
console.info('[iaConfig] guardado', newCfg);
|
| 158 |
+
alert('Configuración guardada');
|
| 159 |
+
});
|
| 160 |
|
| 161 |
+
btnTest.addEventListener('click', async () => {
|
| 162 |
+
try {
|
| 163 |
+
const { callLLM } = await import('./llmClient.js');
|
| 164 |
+
const msg = [
|
| 165 |
+
{ role: 'system', content: 'Eres un asistente útil y conciso.' },
|
| 166 |
+
{ role: 'user', content: 'Di “ok” si me oyes.' }
|
| 167 |
+
];
|
| 168 |
+
const cfgNow = loadConfig();
|
| 169 |
+
const { endpoint, payloadKind } = endpointFor(cfgNow.llm.provider, cfgNow.llm.model);
|
| 170 |
+
const { text } = await callLLM(msg, {
|
| 171 |
+
provider: cfgNow.llm.provider,
|
| 172 |
+
baseUrl: cfgNow.llm.baseUrl,
|
| 173 |
+
apiKey: cfgNow.llm.apiKey,
|
| 174 |
+
endpoint,
|
| 175 |
+
model: cfgNow.llm.model,
|
| 176 |
+
payloadKind
|
| 177 |
+
});
|
| 178 |
+
alert('Respuesta de prueba: ' + (text ?? '(vacío)'));
|
| 179 |
+
} catch (e) {
|
| 180 |
+
console.error(e);
|
| 181 |
+
alert('Fallo en prueba: ' + (e?.message || e));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
}
|