Warda
Travel Designer AI – Garda Outdoors
`; } } msgDiv.innerHTML = processedText; chatBox.appendChild(msgDiv); chatBox.scrollTop = chatBox.scrollHeight; } function handleWardaEnter(e) { if (e.key === 'Enter') sendWardaMessage(); } // 3. Funzione di chiamata API con Polling (esattamente come nel tuo codice) async function sendWardaMessage() { const text = inputField.value.trim(); if (!text) return; appendMessage("user", text); inputField.value = ""; const typingId = "warda-typing-" + Date.now(); const typingIndicator = document.createElement("div"); typingIndicator.className = "warda-typing"; typingIndicator.id = typingId; typingIndicator.innerText = "Warda sta creando la magia (potrebbe volerci un minuto)..."; chatBox.appendChild(typingIndicator); chatBox.scrollTop = chatBox.scrollHeight; try { const response = await fetch("https://api.gardaai.it/api/garda/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ session_id: sessionId, partner_key: "PORTAL_ED4CDDA3", current_message: text }) }); if (response.ok) { const data = await response.json(); // SISTEMA DI POLLING ASINCRONO if (data.status === "processing" && data.task_id) { const pollInterval = setInterval(async () => { try { const pollRes = await fetch(`https://api.gardaai.it/api/garda/poll/${data.task_id}`); if (pollRes.ok) { const pollData = await pollRes.json(); if (pollData.status === "done" || pollData.status === "error") { clearInterval(pollInterval); document.getElementById(typingId)?.remove(); console.log("✅ Dati ricevuti dal server:", pollData); appendMessage("bot", pollData.reply); } } } catch (pollError) { console.error("Errore polling:", pollError); } }, 3000); // Controlla ogni 3 secondi } else if (data.reply) { // Risposta immediata (es. domande del Direttore) document.getElementById(typingId)?.remove(); appendMessage("bot", data.reply); } } else { document.getElementById(typingId)?.remove(); appendMessage("bot", "Scusa, i miei circuiti sono temporaneamente occupati. Riprova tra poco."); } } catch (error) { document.getElementById(typingId)?.remove(); appendMessage("bot", "Errore di connessione al server Warda."); console.error("Fetch Error:", error); } }




