// 💬 JavaScript для БелУзор v0.1 — Беларусь let conversationHistory = []; let isProcessing = false; let currentChatId = null; // Ініцыялізацыя document.addEventListener('DOMContentLoaded', () => { loadStats(); loadLLMStatus(); loadHistory(); loadBooks(); loadEncyclopedias(); loadDictionaries(); loadChatsList(); }); // Аўтаматычнае змяненне памеру textarea function autoResize(textarea) { textarea.style.height = 'auto'; textarea.style.height = Math.min(textarea.scrollHeight, 180) + 'px'; } // Апрацоўка клавіш function handleKeyDown(event) { if (event.key === 'Enter' && !event.shiftKey) { event.preventDefault(); sendMessage(); } } // Адпраўка паведамлення async function sendMessage() { const input = document.getElementById('message-input'); const message = input.value.trim(); if (!message || isProcessing) return; // Схаваць прывітанне, паказаць кантэйнер паведамленняў document.getElementById('welcome-message').style.display = 'none'; document.getElementById('messages-container').classList.add('active'); // Даданне паведамлення карыстальніка addMessage('user', message); // Ачыстка поля input.value = ''; input.style.height = 'auto'; // Адпраўка на сервер isProcessing = true; document.getElementById('btn-send').disabled = true; // Паказ індыкатара загрузкі const loadingId = addLoading(); try { const useLLM = document.getElementById('use-llm').checked; const showFacts = document.getElementById('show-facts').checked; const response = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: message, use_llm: useLLM }) }); if (!response.ok) { throw new Error('Памылка сервера'); } const data = await response.json(); // Выдаленне індыкатара загрузкі removeLoading(loadingId); // Даданне адказу з крыніцамі addMessage('assistant', data.llm_answer, data.facts, showFacts); } catch (error) { removeLoading(loadingId); addMessage('assistant', `❌ Памылка: ${error.message}`); } finally { isProcessing = false; document.getElementById('btn-send').disabled = false; input.focus(); } } // Даданне паведамлення function addMessage(role, text, facts = null, showFacts = true) { const container = document.getElementById('messages-container'); const messageDiv = document.createElement('div'); messageDiv.className = `message message-${role}`; const contentDiv = document.createElement('div'); contentDiv.className = 'message-content'; let html = `

${role === 'user' ? '👤 Вы' : '🤖 БелУзор'}

`; html += `
${escapeHtml(text)}
`; // Калі ёсць факты і яны павінны адлюстроўвацца if (facts && facts.length > 0 && showFacts && role === 'assistant') { html += `
`; html += `
📚 Крыніцы (${facts.length}):
`; facts.forEach((fact, i) => { const googleQuery = encodeURIComponent(fact.substring(0, 100)); // Ачыстка крыніцы ад імёнаў файлаў let cleanFact = fact .replace(/^\[tom_\d+_clean\]\s*/gi, '') // Прыбраць [tom_1_clean] .replace(/^\[encyclopedia_volume_\d+\]\s*/gi, '') // Прыбраць [encyclopedia_volume_1] .replace(/^\[\d+\]\s*/gi, '') // Прыбраць [1] .replace(/^\d+\.\s*/gi, '') // Прыбраць 1. .trim(); // Вызначэнне крыніцы let sourceBadge = ''; if (fact.match(/tom_\d+_clean/i)) { sourceBadge = '📚 Энцыклапедыя'; } else if (fact.match(/^\[\d\]/) || fact.match(/^\d+\./)) { sourceBadge = '📖 Падручнік'; } html += `
[${i+1}] ${sourceBadge} ${escapeHtml(cleanFact)}
`; }); html += `
`; } contentDiv.innerHTML = html; messageDiv.appendChild(contentDiv); container.appendChild(messageDiv); // Пракрутка ўніз container.scrollTop = container.scrollHeight; } // Пошук у Google function searchInGoogle(query) { window.open(`https://www.google.com/search?q=${query}`, '_blank'); } // Пошук усіх крыніц у Google function searchAllInGoogle() { const factTexts = document.querySelectorAll('.fact-item .fact-text'); if (factTexts.length === 0) return; const queries = Array.from(factTexts).map(el => el.textContent.trim()); const combinedQuery = encodeURIComponent(queries.slice(0, 3).join(' OR ')); window.open(`https://www.google.com/search?q=${combinedQuery}`, '_blank'); } // Індыкатар загрузкі function addLoading() { const container = document.getElementById('messages-container'); const id = 'loading-' + Date.now(); const loadingDiv = document.createElement('div'); loadingDiv.className = 'message message-assistant'; loadingDiv.id = id; loadingDiv.innerHTML = `
Пошук адказу па ўзоры кніг...
`; container.appendChild(loadingDiv); container.scrollTop = container.scrollHeight; return id; } function removeLoading(id) { const element = document.getElementById(id); if (element) { element.remove(); } } // Прыклад пытання function sendExample(question) { document.getElementById('message-input').value = question; sendMessage(); } // Новы дыялог function newChat() { // Аўтаматычнае захаванне бягучага дыялогу if (currentChatId || document.querySelectorAll('.message').length > 0) { saveChat(); } document.getElementById('messages-container').innerHTML = ''; document.getElementById('welcome-message').style.display = 'block'; conversationHistory = []; currentChatId = null; // Абнаўленне спісу дыялогаў loadChatsList(); showMessage('Новы дыялог пачаты'); } // Ачыстка гісторыі async function clearHistory() { if (!confirm('Сапраўды ачысціць гісторыю дыялогаў?')) return; try { await fetch('/api/history/clear', { method: 'POST' }); conversationHistory = []; document.getElementById('messages-container').innerHTML = ''; document.getElementById('welcome-message').style.display = 'block'; currentChatId = null; localStorage.removeItem('belUzor_chats'); showMessage('Гісторыя ачышчана'); } catch (error) { showMessage('Памылка: ' + error.message); } } // Загрузка статыстыкі async function loadStats() { try { const response = await fetch('/api/stats'); const stats = await response.json(); document.getElementById('stat-facts').textContent = stats.facts.toLocaleString(); document.getElementById('stat-words').textContent = stats.indexed_words.toLocaleString(); document.getElementById('stat-books').textContent = stats.books_count; } catch (error) { console.error('Памылка загрузкі статыстыкі:', error); } } // Загрузка статусу LLM async function loadLLMStatus() { try { const response = await fetch('/api/llm/status'); const status = await response.json(); const indicator = document.getElementById('llm-indicator'); const text = document.getElementById('llm-text'); const btnEnable = document.getElementById('btn-llm-enable'); const btnDisable = document.getElementById('btn-llm-disable'); if (status.enabled) { indicator.classList.add('active'); text.textContent = `${status.provider} / ${status.model}`; text.style.color = 'var(--success-color)'; btnEnable.style.display = 'none'; btnDisable.style.display = 'block'; } else { indicator.classList.remove('active'); text.textContent = 'Адключаны'; text.style.color = 'var(--text-light)'; btnEnable.style.display = 'block'; btnDisable.style.display = 'none'; } } catch (error) { console.error('Памылка загрузкі LLM статусу:', error); } } // Уключыць LLM async function enableLLM() { openModal(); } // Адключыць LLM async function disableLLM() { if (!confirm('Сапраўды адключыць LLM?')) return; try { await fetch('/api/llm/disable', { method: 'POST' }); document.getElementById('use-llm').checked = false; showMessage('LLM адключаны'); loadLLMStatus(); } catch (error) { showMessage('Памылка: ' + error.message); } } // Модальнае акно function openModal() { document.getElementById('llm-modal').classList.add('active'); } function closeModal() { document.getElementById('llm-modal').classList.remove('active'); } // Захаванне налад LLM async function saveLLMSettings() { const provider = document.getElementById('llm-provider').value; const model = document.getElementById('llm-model').value; const customUrl = document.getElementById('llm-custom-url').value; try { const response = await fetch('/api/llm/enable', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ provider: provider, model: model, custom_url: customUrl }) }); const result = await response.json(); if (response.ok) { showMessage(result.message); document.getElementById('use-llm').checked = true; closeModal(); loadLLMStatus(); } else { showMessage('Памылка: ' + result.error); } } catch (error) { showMessage('Памылка: ' + error.message); } } // Загрузка гісторыі async function loadHistory() { try { const response = await fetch('/api/history'); const data = await response.json(); conversationHistory = data.history || []; // Калі ёсць гісторыя, паказаць яе if (conversationHistory.length > 0) { document.getElementById('welcome-message').style.display = 'none'; document.getElementById('messages-container').classList.add('active'); conversationHistory.forEach(msg => { if (msg.role === 'assistant') { addMessage(msg.role, msg.content, msg.facts, true); } }); } } catch (error) { console.error('Памылка загрузкі гісторыі:', error); } } // Загрузка кніг async function loadBooks() { try { const response = await fetch('/api/books'); const data = await response.json(); const booksList = document.getElementById('books-list'); if (data.books && data.books.length > 0) { booksList.innerHTML = data.books.map(book => `
📕 ${book.name} ${book.size_mb} MB
`).join(''); } else { booksList.innerHTML = '

Кніг няма

'; } } catch (error) { console.error('Памылка загрузкі кніг:', error); document.getElementById('books-list').innerHTML = '

Памылка загрузкі

'; } } // Загрузка энцыклапедый async function loadEncyclopedias() { try { const response = await fetch('/api/encyclopedias'); const data = await response.json(); const encList = document.getElementById('encyclopedia-list'); if (data.encyclopedias && data.encyclopedias.length > 0) { encList.innerHTML = data.encyclopedias.map((enc, i) => `
📖 Том ${i+1} ${enc.size_mb} MB
`).join(''); } else { encList.innerHTML = '

Энцыклапедый няма

'; } } catch (error) { console.error('Памылка загрузкі энцыклапедый:', error); document.getElementById('encyclopedia-list').innerHTML = '

Памылка загрузкі

'; } } // Загрузка слоўнікаў async function loadDictionaries() { try { const response = await fetch('/api/dictionaries'); const data = await response.json(); const dictList = document.getElementById('dictionaries-list'); if (data.dictionaries && data.dictionaries.length > 0) { dictList.innerHTML = data.dictionaries.map(dict => `
📗 ${dict.name} ${dict.count || ''}
`).join(''); } else { dictList.innerHTML = '

Слоўнікаў няма

'; } } catch (error) { console.error('Памылка загрузкі слоўнікаў:', error); document.getElementById('dictionaries-list').innerHTML = '

Памылка загрузкі

'; } } // Прагляд слоўніка async function viewDictionary(dictName) { try { const modal = document.getElementById('dictionary-modal'); const title = document.getElementById('dict-title'); const content = document.getElementById('dictionary-content'); const countLabel = document.getElementById('dict-count'); title.textContent = dictName; content.innerHTML = '

Загрузка...

'; modal.classList.add('active'); const response = await fetch(`/api/dictionaries/${encodeURIComponent(dictName)}`); const data = await response.json(); if (data.entries && data.entries.length > 0) { window.currentDictionary = data.entries; countLabel.textContent = `${data.entries.length} запісаў`; renderDictionary(data.entries); } else { content.innerHTML = '

Запісаў не знойдзена

'; } } catch (error) { showMessage('Памылка загрузкі слоўніка: ' + error.message); closeDictionaryModal(); } } // Рэндэрынг слоўніка function renderDictionary(entries) { const content = document.getElementById('dictionary-content'); if (!entries || entries.length === 0) { content.innerHTML = '

Запісаў не знойдзена

'; return; } content.innerHTML = entries.map(entry => { const word = entry.word || ''; const definition = entry.definition || ''; // Калі ёсць і слова і азначэнне if (word && definition) { return `
${escapeHtml(word)}
${escapeHtml(definition)}
`; } // Калі толькі слова (назва артыкула) else if (word) { return `
${escapeHtml(word)}
`; } // Пусты запіс - прапускаем return ''; }).filter(html => html.trim() !== '').join(''); if (content.innerHTML.trim() === '') { content.innerHTML = '

Запісаў не знойдзена

'; } } // Фільтр слоўніка function filterDictionary() { const query = document.getElementById('dict-search').value.toLowerCase(); if (!window.currentDictionary) return; const filtered = window.currentDictionary.filter(entry => { const word = (entry.word || '').toLowerCase(); const def = (entry.definition || '').toLowerCase(); return word.includes(query) || def.includes(query); }); document.getElementById('dict-count').textContent = `${filtered.length} запісаў`; renderDictionary(filtered); } // Закрыццё модальнага акна слоўніка function closeDictionaryModal() { const modal = document.getElementById('dictionary-modal'); modal.classList.remove('active'); window.currentDictionary = null; document.getElementById('dict-search').value = ''; } // Пераключэнне матэрыялаў function toggleMaterials(type) { const list = document.getElementById(`${type}-list`); const icon = document.getElementById(`${type}-icon`); if (list.style.display === 'none') { list.style.display = 'block'; icon.textContent = '▼'; } else { list.style.display = 'none'; icon.textContent = '▶'; } } // Загрузка спісу дыялогаў function loadChatsList() { const chats = JSON.parse(localStorage.getItem('belUzor_chats') || '[]'); const chatsList = document.getElementById('chats-list'); const chatCount = document.getElementById('chat-count'); chatCount.textContent = `(${chats.length})`; if (chats.length === 0) { chatsList.innerHTML = '

Дыялогаў няма

'; return; } // Сартыроўка па часе (новыя зверху) const sortedChats = chats.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp)); chatsList.innerHTML = sortedChats.map((chat, i) => { const date = new Date(chat.timestamp).toLocaleString('be-BY', { day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit' }); const firstMessage = chat.messages[0]?.content.substring(0, 40) || 'Дыялог'; return ` `; }).join(''); } // Загрузка кніг async function loadBooks() { try { const response = await fetch('/api/books'); const data = await response.json(); const booksList = document.getElementById('books-list'); if (data.books && data.books.length > 0) { booksList.innerHTML = data.books.map(book => `
📖 ${book.name} ${book.size_mb} MB
`).join(''); } else { booksList.innerHTML = '

Кніг няма

'; } } catch (error) { console.error('Памылка загрузкі кніг:', error); document.getElementById('books-list').innerHTML = '

Памылка загрузкі

'; } } // Дадаванне кнігі function addBook() { document.getElementById('file-input').click(); } function handleFileSelect(event) { const file = event.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = async function(e) { const bookText = e.target.result; const bookName = file.name.replace('.txt', ''); try { showMessage(`Апрацоўка кнігі "${bookName}"...`); // Запампоўка на сервер const response = await fetch('/api/books/add', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ book_text: bookText, book_name: bookName, parse_mode: 'sentences' }) }); const result = await response.json(); if (response.ok) { showMessage(`✅ Кніга дададзена: ${result.facts_count} фактаў`); loadBooks(); loadStats(); } else { showMessage('Памылка: ' + result.error); } } catch (error) { showMessage('Памылка: ' + error.message); } }; reader.readAsText(file, 'UTF-8'); // Ачыстка input для паўторнага выбару таго ж файла event.target.value = ''; } // Захаванне дыялогу function saveChat() { const messages = document.querySelectorAll('.message'); if (messages.length === 0) { showMessage('Няма чаго захоўваць'); return; } const chatData = { id: Date.now(), timestamp: new Date().toISOString(), messages: [] }; messages.forEach(msg => { const isUser = msg.classList.contains('message-user'); const content = msg.querySelector('.message-text').textContent; chatData.messages.push({ role: isUser ? 'user' : 'assistant', content: content, timestamp: new Date().toISOString() }); }); // Захаванне ў localStorage let chats = JSON.parse(localStorage.getItem('belUzor_chats') || '[]'); chats.push(chatData); localStorage.setItem('belUzor_chats', JSON.stringify(chats)); currentChatId = chatData.id; showMessage('Дыялог захаваны'); } // Загрузка дыялогу function loadChat() { const chats = JSON.parse(localStorage.getItem('belUzor_chats') || '[]'); if (chats.length === 0) { showMessage('Захаваных дыялогаў няма'); return; } // Стварэнне спісу дыялогаў const chatList = chats.map((chat, i) => { const date = new Date(chat.timestamp).toLocaleString('be-BY'); return `
Дыялог #${i + 1}
${date}
${chat.messages.length} паведамленняў
`; }).join(''); // Паказ модальнага акна са спісам const modal = document.createElement('div'); modal.className = 'modal active'; modal.innerHTML = ` `; document.body.appendChild(modal); } // Выдаленне дыялогу function deleteChat(chatId) { if (!confirm('Выдаліць гэты дыялог?')) return; let chats = JSON.parse(localStorage.getItem('belUzor_chats') || '[]'); chats = chats.filter(c => c.id !== chatId); localStorage.setItem('belUzor_chats', JSON.stringify(chats)); // Абнаўленне спісу ў бакавой панэлі loadChatsList(); // Выдаленне з мадальнага акна const chatItem = document.getElementById(`chat-${chatId}`); if (chatItem) chatItem.remove(); showMessage('Дыялог выдалены'); } // Выдаленне ўсіх дыялогаў function deleteAllChats() { if (!confirm('Сапраўды выдаліць УСЕ дыялогі?')) return; localStorage.removeItem('belUzor_chats'); // Зачыненне модальнага акна document.querySelector('.modal').remove(); showMessage('Усе дыялогі выдалены'); } // Загрузка канкрэтнага дыялогу function loadSpecificChat(chatId) { const chats = JSON.parse(localStorage.getItem('belUzor_chats') || '[]'); const chat = chats.find(c => c.id === chatId); if (!chat) return; document.getElementById('messages-container').innerHTML = ''; document.getElementById('welcome-message').style.display = 'none'; document.getElementById('messages-container').classList.add('active'); chat.messages.forEach(msg => { addMessage(msg.role, msg.content, null, false); }); currentChatId = chatId; // Зачыненне модальнага акна document.querySelector('.modal').remove(); showMessage('Дыялог загружаны'); } // Захаванне ў localStorage function saveChatToStorage() { if (currentChatId) { saveChat(); } } // Экспарт дыялогу function exportChat() { const messages = document.querySelectorAll('.message'); if (messages.length === 0) { showMessage('Няма чаго экспартаваць'); return; } let text = '🧠🇧🇾 БелУзор v0.1 — Дыялог\n'; text += '=' .repeat(60) + '\n'; text += `Дата: ${new Date().toLocaleString('be-BY')}\n`; text += '=' .repeat(60) + '\n\n'; messages.forEach(msg => { const role = msg.classList.contains('message-user') ? 'Вы' : 'БелУзор'; const content = msg.querySelector('.message-text').textContent; text += `${role}: ${content}\n\n`; }); text += '\n' + '=' .repeat(60) + '\n'; text += 'Працуем па ўзоры давераных кніг\n'; const blob = new Blob([text], { type: 'text/plain;charset=utf-8' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `БелУзор_дыялог_${new Date().toISOString().split('T')[0]}.txt`; a.click(); URL.revokeObjectURL(url); showMessage('Дыялог экспартаваны'); } // Паведамленне function showMessage(text) { const existing = document.querySelector('.toast'); if (existing) existing.remove(); const div = document.createElement('div'); div.className = 'toast'; div.textContent = text; document.body.appendChild(div); setTimeout(() => { div.style.animation = 'fadeOut 0.3s'; setTimeout(() => div.remove(), 300); }, 3000); } // Escape HTML function escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } // Даданне анімацый const style = document.createElement('style'); style.textContent = ` @keyframes fadeOut { from { opacity: 1; transform: translateY(0); } to { opacity: 0; transform: translateY(20px); } } `; document.head.appendChild(style);