|
| 1 | +document.addEventListener('DOMContentLoaded', () => { |
| 2 | + const urlParams = new URLSearchParams(window.location.search); |
| 3 | + const pdfPath = urlParams.get('path'); |
| 4 | + const pdfTitle = urlParams.get('title') || 'PDF Document'; |
| 5 | + |
| 6 | + if (!pdfPath) { |
| 7 | + showSystemMessage('Error: No PDF document specified.', true); |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + document.getElementById('pdf-title').textContent = `Chat: ${pdfTitle}`; |
| 12 | + |
| 13 | + // Initialize chatbot |
| 14 | + initChatbot(pdfPath); |
| 15 | + |
| 16 | + const chatForm = document.getElementById('chat-form'); |
| 17 | + chatForm.addEventListener('submit', async (e) => { |
| 18 | + e.preventDefault(); |
| 19 | + const input = document.getElementById('chat-input'); |
| 20 | + const question = input.value.trim(); |
| 21 | + if (!question) return; |
| 22 | + |
| 23 | + // Add user message to UI |
| 24 | + addMessage(question, 'user'); |
| 25 | + input.value = ''; |
| 26 | + |
| 27 | + // Disable input while waiting |
| 28 | + setFormState(false); |
| 29 | + |
| 30 | + // Show typing indicator |
| 31 | + const typingId = addTypingIndicator(); |
| 32 | + |
| 33 | + try { |
| 34 | + const response = await fetch('/api/chat/ask', { |
| 35 | + method: 'POST', |
| 36 | + headers: { 'Content-Type': 'application/json' }, |
| 37 | + body: JSON.stringify({ path: pdfPath, question: question }) |
| 38 | + }); |
| 39 | + |
| 40 | + const data = await response.json(); |
| 41 | + |
| 42 | + // Remove typing indicator |
| 43 | + removeElement(typingId); |
| 44 | + |
| 45 | + if (data.success) { |
| 46 | + addMessage(data.answer, 'ai'); |
| 47 | + } else { |
| 48 | + addMessage(`Error: ${data.message}`, 'ai'); |
| 49 | + } |
| 50 | + } catch (error) { |
| 51 | + removeElement(typingId); |
| 52 | + addMessage('Failed to connect to the server. Please try again.', 'ai'); |
| 53 | + } |
| 54 | + |
| 55 | + // Re-enable input |
| 56 | + setFormState(true); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +async function initChatbot(path) { |
| 61 | + try { |
| 62 | + const response = await fetch('/api/chat/init', { |
| 63 | + method: 'POST', |
| 64 | + headers: { 'Content-Type': 'application/json' }, |
| 65 | + body: JSON.stringify({ path: path }) |
| 66 | + }); |
| 67 | + const data = await response.json(); |
| 68 | + |
| 69 | + const initMessageElem = document.getElementById('init-message'); |
| 70 | + if (data.success) { |
| 71 | + initMessageElem.innerHTML = `<i class="fas fa-check-circle" style="color: #4CAF50;"></i> Document analyzed successfully! You can now ask questions.`; |
| 72 | + setFormState(true); |
| 73 | + } else { |
| 74 | + initMessageElem.innerHTML = `<i class="fas fa-exclamation-circle" style="color: #f44336;"></i> Initialization failed: ${data.message}`; |
| 75 | + } |
| 76 | + } catch (error) { |
| 77 | + document.getElementById('init-message').innerHTML = `<i class="fas fa-exclamation-circle" style="color: #f44336;"></i> Server connection failed.`; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function setFormState(enabled) { |
| 82 | + document.getElementById('chat-input').disabled = !enabled; |
| 83 | + document.getElementById('send-btn').disabled = !enabled; |
| 84 | + if (enabled) { |
| 85 | + document.getElementById('chat-input').focus(); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function addMessage(text, sender) { |
| 90 | + const box = document.getElementById('chat-box'); |
| 91 | + const msgDiv = document.createElement('div'); |
| 92 | + msgDiv.className = `message ${sender}-message`; |
| 93 | + |
| 94 | + // Simple markdown to HTML for AI messages (bold, newlines) |
| 95 | + let formattedText = text; |
| 96 | + if (sender === 'ai') { |
| 97 | + formattedText = formattedText |
| 98 | + .replace(/\\*\\*(.*?)\\*\\*/g, '<strong>$1</strong>') |
| 99 | + .replace(/\\n/g, '<br>'); |
| 100 | + } |
| 101 | + |
| 102 | + msgDiv.innerHTML = formattedText; |
| 103 | + box.appendChild(msgDiv); |
| 104 | + box.scrollTop = box.scrollHeight; |
| 105 | +} |
| 106 | + |
| 107 | +function addTypingIndicator() { |
| 108 | + const box = document.getElementById('chat-box'); |
| 109 | + const msgDiv = document.createElement('div'); |
| 110 | + const id = 'typing-' + Date.now(); |
| 111 | + msgDiv.id = id; |
| 112 | + msgDiv.className = `message ai-message`; |
| 113 | + msgDiv.innerHTML = '<i class="fas fa-circle-notch fa-spin"></i> Generating answer...'; |
| 114 | + box.appendChild(msgDiv); |
| 115 | + box.scrollTop = box.scrollHeight; |
| 116 | + return id; |
| 117 | +} |
| 118 | + |
| 119 | +function removeElement(id) { |
| 120 | + const el = document.getElementById(id); |
| 121 | + if (el) el.remove(); |
| 122 | +} |
0 commit comments