|
|
const chat = document.getElementById('chat');
|
|
|
const input = document.getElementById('query');
|
|
|
const sendBtn = document.getElementById('sendBtn');
|
|
|
|
|
|
function addMessage(text, sender, isPre = false) {
|
|
|
const msg = document.createElement('div');
|
|
|
msg.classList.add('message', sender);
|
|
|
|
|
|
|
|
|
if (isPre) {
|
|
|
msg.classList.add('pre-formatted');
|
|
|
|
|
|
|
|
|
const pre = document.createElement('pre');
|
|
|
pre.textContent = text;
|
|
|
|
|
|
|
|
|
msg.appendChild(pre);
|
|
|
} else {
|
|
|
msg.textContent = text;
|
|
|
}
|
|
|
|
|
|
chat.appendChild(msg);
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
msg.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
|
|
}, 100);
|
|
|
|
|
|
return msg;
|
|
|
}
|
|
|
|
|
|
async function sendMessage() {
|
|
|
const query = input.value.trim();
|
|
|
if (!query) return;
|
|
|
|
|
|
const bg = document.getElementById('chat-background');
|
|
|
if (bg && !bg.classList.contains('blurred')) {
|
|
|
bg.classList.add('blurred');
|
|
|
}
|
|
|
|
|
|
addMessage(query, 'user');
|
|
|
input.value = '';
|
|
|
|
|
|
const loader = addMessage('Processing...', 'bot');
|
|
|
|
|
|
try {
|
|
|
const response = await fetch('/api/detect', {
|
|
|
method: 'POST',
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
body: JSON.stringify({ query: query })
|
|
|
});
|
|
|
|
|
|
const data = await response.json();
|
|
|
loader.remove();
|
|
|
|
|
|
if (data && data.summary) {
|
|
|
|
|
|
addMessage(data.summary, 'bot', true);
|
|
|
} else {
|
|
|
addMessage("Could not generate a summary.", 'bot');
|
|
|
}
|
|
|
} catch (e) {
|
|
|
loader.remove();
|
|
|
addMessage("Error checking news.", 'bot');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
function formatBackendData(data) {
|
|
|
|
|
|
if (data && data.summary) {
|
|
|
if (typeof data.summary === 'string') {
|
|
|
return data.summary;
|
|
|
} else if (typeof data.summary === 'object' && data.summary.text) {
|
|
|
return data.summary.text;
|
|
|
} else {
|
|
|
return JSON.stringify(data.summary, null, 2);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
sendBtn.addEventListener('click', sendMessage);
|
|
|
input.addEventListener('keypress', (e) => {
|
|
|
if (e.key === 'Enter') sendMessage();
|
|
|
});
|
|
|
|