Spaces:
Runtime error

Asistente-3D / static /app.js
Daniel1812's picture
Update static/app.js
2834243 verified
Raw History Blame Contribute Delete
1.3 kB
// ====== UI helpers ======
const chatEl = document.getElementById("chat");
const form = document.getElementById("chatForm");
const input = document.getElementById("msgInput");
function addMsg(text, who = "bot") {
const div = document.createElement("div");
div.className = `msg ${who}`;
div.textContent = text;
chatEl.appendChild(div);
chatEl.scrollTop = chatEl.scrollHeight;
}
// ====== Llamada al backend ======
async function askBackend(message) {
const resp = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message })
});
if (!resp.ok) throw new Error("Error en el servidor");
const data = await resp.json();
return data.reply || "No tengo respuesta ahora.";
}
// ====== Manejo del formulario ======
form.addEventListener("submit", async (e) => {
e.preventDefault();
const text = input.value.trim();
if (!text) return;
addMsg(text, "user");
input.value = "";
addMsg("Pensando...");
try {
const reply = await askBackend(text);
// quita "Pensando..."
chatEl.lastElementChild.remove();
addMsg(reply, "bot");
} catch (err) {
chatEl.lastElementChild.remove();
addMsg("Hubo un problema al responder. Intenta de nuevo.", "bot");
console.error(err);
}
});