async function sendMessage(message, messageList, userName, token) { if (!token) { return "not_init"; } messageList.push({ role: "user", content: message }); const cuerpo = { messages: messageList, user: userName } const response = await fetch("/api/chat/completions", { method: "POST", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}` }, body: JSON.stringify(cuerpo) }); if (!response.ok) { const errorData = await response.json().catch(() => ({ message: "Respuesta no válida del servidor." })); throw new Error(errorData.message || `Error del servidor: ${response.status}`); } const data = await response.json(); const assistantResponse = data.response; if (assistantResponse) { messageList.push({ role: "assistant", content: assistantResponse }); return { messageList, assistantResponse }; } else { throw new Error("Respuesta vacía del asistente."); } } export { sendMessage };