Spaces:
Sleeping
Sleeping
File size: 4,031 Bytes
41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 82b5c1f 41e2a58 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | const launcher = document.getElementById('askross-launcher');
const openChat = document.getElementById('open-chat');
const closeChat = document.getElementById('close-chat');
const chatbotContainer = document.getElementById('chatbot-container');
const chatForm = document.getElementById('chat-form');
const userInput = document.getElementById('user-input');
const chatMessages = document.getElementById('chat-messages');
const typingIndicator = document.getElementById('typing-indicator');
let thread_id = localStorage.getItem('askross_thread_id');
if (!thread_id) {
thread_id = crypto.randomUUID();
localStorage.setItem('askross_thread_id', thread_id);
}
// Page context
const pageContext = {
page_id: window.location.pathname === '/' ? 'home' : 'page',
page_title: document.title || 'AskRoss',
};
let isFirstMessage = true;
// Suggestions
const homepageQuestions = [
"How can I get a mortgage if the bank said no?",
"What mortgage services do you offer?",
"Can I refinance my home?",
"How do I improve my credit score?"
];
// Show suggestions
function showSuggestions() {
const wrapper = document.createElement('div');
wrapper.className = 'suggestions-wrapper';
homepageQuestions.forEach(q => {
const btn = document.createElement('button');
btn.className = 'suggestion-btn';
btn.textContent = q;
btn.onclick = () => sendMessage(q);
wrapper.appendChild(btn);
});
chatMessages.appendChild(wrapper);
}
// Open chat
openChat.addEventListener('click', () => {
chatbotContainer.classList.add('open');
launcher.style.display = 'none';
if (chatMessages.children.length === 0) {
showSuggestions();
}
});
// Close chat
closeChat.addEventListener('click', () => {
chatbotContainer.classList.remove('open');
launcher.style.display = 'flex';
});
// Send message
async function sendMessage(message) {
if (!message) return;
// Remove suggestions only once
if (isFirstMessage) {
chatMessages.innerHTML = '';
isFirstMessage = false;
}
appendMessage('user', message);
typingIndicator.classList.remove('hidden');
scrollToBottom();
try {
const response = await fetch('/chat', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
message,
thread_id,
page_id: pageContext.page_id,
page_title: pageContext.page_title
})
});
const data = await response.json();
typingIndicator.classList.add('hidden');
appendMessage('bot', data.message || "Sorry, something went wrong");
} catch (err) {
typingIndicator.classList.add('hidden');
appendMessage('bot', "Network error. Please try again.");
}
scrollToBottom();
}
// Form submit
chatForm.addEventListener('submit', (e) => {
e.preventDefault();
const message = userInput.value.trim();
if (!message) return;
userInput.value = '';
sendMessage(message);
});
// Markdown support
function markdownToHtml(md) {
return md
.replace(/&/g,'&')
.replace(/</g,'<')
.replace(/>/g,'>')
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/\*(.*?)\*/g, '<em>$1</em>')
.replace(/\n/g, '<br>')
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank">$1</a>');
}
// Append message
function appendMessage(sender, text) {
const wrapper = document.createElement('div');
wrapper.className = `message ${sender}`;
const bubble = document.createElement('div');
bubble.className = 'bubble';
if (sender === 'bot') {
bubble.innerHTML = markdownToHtml(text);
} else {
bubble.textContent = text;
}
wrapper.appendChild(bubble);
chatMessages.appendChild(wrapper);
}
// Scroll helper
function scrollToBottom() {
chatMessages.scrollTop = chatMessages.scrollHeight;
} |