// FinBot Frontend Application JavaScript const API_BASE = "http://localhost:8000/api"; // State let currentUser = null; let users = []; let userCollections = []; // Initialize app document.addEventListener("DOMContentLoaded", async () => { console.log("FinBot Frontend Initializing..."); // Check if already logged in const savedUser = localStorage.getItem("currentUser"); if (savedUser) { const user = JSON.parse(savedUser); await loginUser(user.username); } else { // Show login screen await loadUsers(); showLoginScreen(); } }); // Load users for login dropdown async function loadUsers() { try { const response = await fetch(`${API_BASE}/users`); if (!response.ok) throw new Error("Failed to load users"); users = await response.json(); // Populate dropdown const select = document.getElementById("userSelect"); select.innerHTML = ''; users.forEach(user => { const option = document.createElement("option"); option.value = user.username; option.textContent = `${user.name} (${user.role})`; select.appendChild(option); }); // Populate login buttons const usersList = document.getElementById("usersList"); usersList.innerHTML = ""; users.forEach(user => { const btn = document.createElement("button"); btn.className = "user-btn"; btn.onclick = () => loginUser(user.username); btn.innerHTML = ` ${user.name} ${user.role} • ${user.department} `; usersList.appendChild(btn); }); } catch (error) { console.error("Error loading users:", error); showError("Failed to load user list. Check that the backend is running."); } } // Show login screen function showLoginScreen() { document.getElementById("loginScreen").style.display = "flex"; document.getElementById("chatInterface").style.display = "none"; } // Show chat interface function showChatInterface() { document.getElementById("loginScreen").style.display = "none"; document.getElementById("chatInterface").style.display = "flex"; } // Login user async function loginUser(username) { try { const response = await fetch(`${API_BASE}/users/${username}`); if (!response.ok) throw new Error("User not found"); currentUser = await response.json(); userCollections = currentUser.accessible_collections; // Save to localStorage localStorage.setItem("currentUser", JSON.stringify(currentUser)); // Update UI document.getElementById("userName").textContent = currentUser.name; document.getElementById("userRole").textContent = currentUser.role; document.getElementById("userDept").textContent = currentUser.department; // Update collections display updateCollectionsDisplay(); // Clear chat and show interface document.getElementById("chatMessages").innerHTML = `
`; showChatInterface(); document.getElementById("queryInput").focus(); } catch (error) { console.error("Error logging in:", error); showError(`Failed to login user: ${error.message}`); } } // Logout user function logout() { currentUser = null; userCollections = []; localStorage.removeItem("currentUser"); document.getElementById("userSelect").value = ""; showLoginScreen(); } // Switch user via dropdown async function switchUser() { const username = document.getElementById("userSelect").value; if (username) { await loginUser(username); } } // Update collections display function updateCollectionsDisplay() { const collectionsDiv = document.getElementById("collections"); collectionsDiv.innerHTML = ""; userCollections.forEach(collection => { const tag = document.createElement("div"); tag.className = "collection-tag"; tag.textContent = collection.toUpperCase(); collectionsDiv.appendChild(tag); }); } // Send query async function sendQuery() { const queryInput = document.getElementById("queryInput"); const query = queryInput.value.trim(); if (!query) return; if (!currentUser) { showError("Please login first"); return; } // Add user message to chat addMessageToChat(query, "user"); queryInput.value = ""; // Show loading indicator showLoadingIndicator(true); try { // Call API const response = await fetch(`${API_BASE}/chat`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ user_role: currentUser.role, query: query, user_id: currentUser.username, }), }); if (!response.ok) { throw new Error(`API Error: ${response.statusText}`); } const result = await response.json(); // Hide loading showLoadingIndicator(false); // Display guardrail warnings if any if (result.guardrail_warnings && result.guardrail_warnings.length > 0) { showGuardrailWarnings(result.guardrail_warnings); } else { hideGuardrailWarnings(); } // Handle RBAC denial if (result.rbac_denied) { addMessageToChat( `🔒 Access Denied