const API_URL = "/analyze"; console.log("JS LOADED"); console.log("API URL NOW =", API_URL) const chatMessages = document.getElementById("chatMessages"); const chatForm = document.getElementById("chatForm"); const chatInput = document.getElementById("chatInput"); const choicePanel = document.getElementById("choicePanel"); const writeOwnBtn = document.getElementById("writeOwnBtn"); const choiceButtons = document.querySelectorAll(".choice-btn[data-example]"); const sendBtn = document.getElementById("sendBtn"); const typingIndicator = document.getElementById("typingIndicator"); let pendingClarification = false; let currentComplaint = ""; /** * Escapes HTML characters to prevent XSS. */ function escapeHtml(text) { if (text === null || text === undefined) return ""; return String(text) .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">"); } /** * Auto-scroll to the bottom of the chat container. */ function scrollToBottom() { chatMessages.scrollTop = chatMessages.scrollHeight; } /** * Appends a user message to the chat. */ function appendUserMessage(text) { const msgDiv = document.createElement("div"); msgDiv.className = "message user"; const contentDiv = document.createElement("div"); contentDiv.className = "message-content"; contentDiv.innerText = text; msgDiv.appendChild(contentDiv); chatMessages.appendChild(msgDiv); scrollToBottom(); } /** * Appends a bot message (HTML supported) to the chat. */ function appendBotMessage(htmlContent) { const msgDiv = document.createElement("div"); msgDiv.className = "message bot"; const contentDiv = document.createElement("div"); contentDiv.className = "message-content"; contentDiv.innerHTML = htmlContent; msgDiv.appendChild(contentDiv); chatMessages.appendChild(msgDiv); scrollToBottom(); } function showTyping() { typingIndicator.classList.remove("hidden"); scrollToBottom(); } function hideTyping() { typingIndicator.classList.add("hidden"); } /** * Builds the HTML content for the bot's response incorporating result cards. */ function buildResultUI(data) { let html = ""; // 1. Check if clarification is needed if (data.clarification_needed && data.clarification_question) { html += `

I need one quick detail before proceeding:

${escapeHtml(data.clarification_question)}

`; pendingClarification = true; return html; } // Reset clarification state if not needed pendingClarification = false; // 2. Normal Response Report html += `

Here’s what I found based on your complaint:

`; if (Array.isArray(data.issues) && data.issues.length > 0) { data.issues.forEach(issue => { const urgencyClass = issue.urgency ? issue.urgency.toLowerCase() : "default"; html += `
${escapeHtml(issue.title)}
${escapeHtml(issue.urgency || "UNKNOWN URGENCY")}
Assigned Authority: ${escapeHtml(issue.authority || "General Administration")}
${escapeHtml(issue.reason)}
`; const rec = data.final_recommendation || issue.recommendation; if (rec) { html += `
Recommendation Action ${escapeHtml(rec)}
`; } html += `
`; }); } else { html += `

We could not identify a valid issue from the request. Please provide more detailed information.

`; } return html; } /** * Handle form submission */ choiceButtons.forEach(button => { button.addEventListener("click", () => { const example = button.dataset.example; chatInput.value = example; chatForm.classList.remove("hidden"); choicePanel.classList.add("hidden"); chatInput.focus(); }); }); writeOwnBtn.addEventListener("click", () => { chatInput.value = ""; chatForm.classList.remove("hidden"); choicePanel.classList.add("hidden"); chatInput.focus(); }); chatForm.addEventListener("submit", async (e) => { e.preventDefault(); const text = chatInput.value.trim(); if (!text) return; // Render User Message appendUserMessage(text); // Clear & Disable Input chatInput.value = ""; chatInput.disabled = true; sendBtn.disabled = true; // Show Typing UI showTyping(); try { let payload = {}; if (pendingClarification) { payload = { complaint: currentComplaint, clarification: text, task_id: "" }; } else { currentComplaint = text; // Keep track of the original context payload = { complaint: text, task_id: "" }; } // Optional delay for a natural feel await new Promise(res => setTimeout(res, 600)); const response = await fetch(API_URL, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({complaint: currentComplaint || text, clarification: pendingClarification ? text: "", task_id: "demo-task" }) }); if (!response.ok) { throw new Error("Server response was not OK."); } const data = await response.json(); hideTyping(); const botHtml = buildResultUI(data); appendBotMessage(botHtml); } catch (err) { console.error("API Error:", err); hideTyping(); appendBotMessage(`

Frontend error: ${escapeHtml(err.message)}

`); } finally { chatInput.disabled = false; sendBtn.disabled = false; chatInput.focus(); } });