// Aria Chat Widget - Dealspace Product Assistant (function() { 'use strict'; // Generate or retrieve session ID function getSessionId() { let sessionId = sessionStorage.getItem('aria_session_id'); if (!sessionId) { sessionId = 'aria_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9); sessionStorage.setItem('aria_session_id', sessionId); } return sessionId; } // Chat state const state = { isOpen: false, isLoading: false, history: [], sessionId: getSessionId() }; // Create chat widget HTML function createWidget() { // Chat button const button = document.createElement('button'); button.id = 'aria-chat-button'; button.setAttribute('aria-label', 'Open chat with Aria'); button.innerHTML = ` `; // Chat panel const panel = document.createElement('div'); panel.id = 'aria-chat-panel'; panel.innerHTML = `
A

Aria

Dealspace Assistant

`; document.body.appendChild(button); document.body.appendChild(panel); // Event listeners button.addEventListener('click', toggleChat); document.getElementById('aria-close-btn').addEventListener('click', toggleChat); document.getElementById('aria-send-btn').addEventListener('click', sendMessage); document.getElementById('aria-message-input').addEventListener('keypress', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } }); } function toggleChat() { const panel = document.getElementById('aria-chat-panel'); state.isOpen = !state.isOpen; if (state.isOpen) { panel.classList.add('open'); // Show welcome message if no history if (state.history.length === 0) { addMessage("Hi, I'm Aria! I can answer questions about Dealspace — features, pricing, security, or how it works. What would you like to know?", 'assistant'); } document.getElementById('aria-message-input').focus(); } else { panel.classList.remove('open'); } } function addMessage(content, role) { const messagesContainer = document.getElementById('aria-chat-messages'); const messageDiv = document.createElement('div'); messageDiv.className = 'aria-message ' + role; messageDiv.textContent = content; messagesContainer.appendChild(messageDiv); messagesContainer.scrollTop = messagesContainer.scrollHeight; // Store in history (exclude welcome message) if (role !== 'assistant' || state.history.length > 0 || content !== "Hi, I'm Aria! I can answer questions about Dealspace — features, pricing, security, or how it works. What would you like to know?") { state.history.push({ role: role, content: content }); // Keep only last 6 messages if (state.history.length > 6) { state.history = state.history.slice(-6); } } } function showTyping() { const messagesContainer = document.getElementById('aria-chat-messages'); const typingDiv = document.createElement('div'); typingDiv.id = 'aria-typing-indicator'; typingDiv.className = 'aria-typing'; typingDiv.innerHTML = ''; messagesContainer.appendChild(typingDiv); messagesContainer.scrollTop = messagesContainer.scrollHeight; } function hideTyping() { const typingIndicator = document.getElementById('aria-typing-indicator'); if (typingIndicator) { typingIndicator.remove(); } } async function sendMessage() { const input = document.getElementById('aria-message-input'); const sendBtn = document.getElementById('aria-send-btn'); const message = input.value.trim(); if (!message || state.isLoading) return; // Add user message addMessage(message, 'user'); input.value = ''; // Show loading state state.isLoading = true; sendBtn.disabled = true; showTyping(); try { const response = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_id: state.sessionId, message: message, history: state.history.slice(0, -1) // Exclude the message we just added }) }); hideTyping(); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Something went wrong'); } const data = await response.json(); addMessage(data.reply, 'assistant'); } catch (error) { hideTyping(); console.error('Chat error:', error); addMessage("Sorry, I'm having trouble connecting. Please try again in a moment.", 'assistant'); } finally { state.isLoading = false; sendBtn.disabled = false; input.focus(); } } // Initialize when DOM is ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', createWidget); } else { createWidget(); } })();