File size: 15,149 Bytes
c02fe07
 
6bf47a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c02fe07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bf47a1
 
 
 
 
c02fe07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
let chatHistory = [];
let messageCount = 0;
let useGemini = false; // Track current LLM choice

// Initialize Gemini toggle
document.addEventListener('DOMContentLoaded', function() {
    const geminiToggle = document.getElementById('geminiToggle');
    const toggleLabel = document.querySelector('.toggle-label');
    
    // Load saved preference
    useGemini = localStorage.getItem('useGemini') === 'true';
    geminiToggle.checked = useGemini;
    updateToggleLabel();
    
    // Handle toggle changes
    geminiToggle.addEventListener('change', function() {
        useGemini = this.checked;
        localStorage.setItem('useGemini', useGemini.toString());
        updateToggleLabel();
        console.log(`Switched to ${useGemini ? 'Gemini' : 'Ollama'} mode`);
        
        // Show confirmation
        showStatus(`Switched to ${useGemini ? 'Gemini (Cloud AI)' : 'Ollama (Local AI)'} mode`, 'info');
        
        // Refresh status to reflect changes
        checkStatus();
    });
});

function updateToggleLabel() {
    const toggleLabel = document.querySelector('.toggle-label');
    if (toggleLabel) {
        toggleLabel.textContent = `AI Model: ${useGemini ? 'Gemini' : 'Ollama'}`;
    }
}

async function checkStatus() {
    try {
        const response = await fetch('/status');
        const status = await response.json();
        
        const statusDiv = document.getElementById('status');
        
        if (status.enabled && status.gemini_configured) {
            statusDiv.className = 'status online';
            statusDiv.innerHTML = '<span>Research systems online</span>' +
                '<div style="margin-top: 0.5rem; font-size: 0.85rem; opacity: 0.8;">' +
                'Tools: ' + status.tools_available.join(' • ') + '</div>';
        } else {
            statusDiv.className = 'status offline';
            statusDiv.innerHTML = '<span>Limited mode - Configure GEMINI_API_KEY for full functionality</span>' +
                '<div style="margin-top: 0.5rem; font-size: 0.85rem; opacity: 0.8;">' +
                'Available: ' + status.tools_available.join(' • ') + '</div>';
        }
    } catch (error) {
        const statusDiv = document.getElementById('status');
        statusDiv.className = 'status offline';
        statusDiv.innerHTML = '<span>Connection error</span>';
    }
}

async function sendQuery() {
    const input = document.getElementById('queryInput');
    const sendBtn = document.getElementById('sendBtn');
    const loadingIndicator = document.getElementById('loadingIndicator');
    const statusIndicator = document.getElementById('statusIndicator');
    const statusText = document.getElementById('statusText');
    const query = input.value.trim();

    if (!query) {
        showStatus('Please enter a research query', 'warning');
        return;
    }

    console.log('Sending research query');
    addMessage('user', query);
    input.value = '';

    // Update UI states
    sendBtn.disabled = true;
    sendBtn.innerHTML = '<span class="loading">Processing</span>';
    loadingIndicator.classList.add('active');
    showStatus('Initializing research...', 'processing');

    try {
        console.log('Starting streaming API request...');
        const requestStart = Date.now();
        
        // Create an AbortController for manual timeout control
        const controller = new AbortController();
        const timeoutId = setTimeout(() => {
            console.log('Manual timeout after 5 minutes');
            controller.abort();
        }, 300000); // 5 minute timeout instead of default browser timeout
        
        // Use fetch with streaming for POST requests with body
        const response = await fetch('/query/stream', {
            method: 'POST',
            headers: { 
                'Content-Type': 'application/json',
                'Accept': 'text/event-stream',
                'Cache-Control': 'no-cache'
            },
            body: JSON.stringify({ 
                query, 
                chat_history: chatHistory, 
                use_gemini: useGemini 
            }),
            signal: controller.signal,
            // Disable browser's default timeout behavior
            keepalive: true
        });

        // Clear the timeout since we got a response
        clearTimeout(timeoutId);

        if (!response.ok) {
            throw new Error('Request failed with status ' + response.status);
        }

        const reader = response.body.getReader();
        const decoder = new TextDecoder();
        let buffer = '';

        while (true) {
            const { done, value } = await reader.read();
            if (done) break;

            buffer += decoder.decode(value, { stream: true });
            const lines = buffer.split('\n');
            buffer = lines.pop(); // Keep incomplete line in buffer

            for (const line of lines) {
                if (line.startsWith('data: ')) {
                    try {
                        const data = JSON.parse(line.substring(6));
                        
                        if (data.type === 'status') {
                            showStatus(data.message, 'processing');
                            updateProgress(data.progress);
                            // Also update the loading text
                            const loadingText = document.getElementById('loadingText');
                            if (loadingText) {
                                loadingText.textContent = data.message;
                            }
                            console.log('Progress: ' + data.progress + '% - ' + data.message);
                        } else if (data.type === 'tools') {
                            showStatus(data.message, 'processing');
                            // Update loading text for tools
                            const loadingText = document.getElementById('loadingText');
                            if (loadingText) {
                                loadingText.textContent = data.message;
                            }
                            console.log('Tools: ' + data.message);
                        } else if (data.type === 'result') {
                            const result = data.data;
                            const requestTime = Date.now() - requestStart;
                            console.log('Request completed in ' + requestTime + 'ms');
                            
                            if (result.success) {
                                addMessage('assistant', result.response, result.sources, result.visualizations);
                                showStatus('Research complete', 'success');
                                console.log('Analysis completed successfully');
                            } else {
                                console.log('Analysis request failed');
                                addMessage('assistant', result.response || 'Analysis temporarily unavailable. Please try again.', [], []);
                                showStatus('Request failed', 'error');
                            }
                        } else if (data.type === 'complete') {
                            break;
                        } else if (data.type === 'error') {
                            throw new Error(data.message);
                        }
                    } catch (parseError) {
                        console.error('Parse error:', parseError);
                    }
                }
            }
        }

    } catch (error) {
        console.error('Streaming request error:', error);
        
        // More specific error handling
        if (error.name === 'AbortError') {
            addMessage('assistant', 'Request timed out after 5 minutes. Ollama may be processing a complex query. Please try a simpler question or wait and try again.');
            showStatus('Request timed out', 'error');
        } else if (error.message.includes('Failed to fetch') || error.message.includes('network error')) {
            addMessage('assistant', 'Network connection error. Please check your internet connection and try again.');
            showStatus('Connection error', 'error');
        } else if (error.message.includes('ERR_HTTP2_PROTOCOL_ERROR')) {
            addMessage('assistant', 'Ollama is still processing your request in the background. Please wait a moment and try again, or try a simpler query.');
            showStatus('Processing - please retry', 'warning');
        } else {
            addMessage('assistant', 'Connection error. Please check your network and try again.');
            showStatus('Connection error', 'error');
        }
    } finally {
        // Reset UI states
        sendBtn.disabled = false;
        sendBtn.innerHTML = 'Research';
        loadingIndicator.classList.remove('active');
        input.focus();
        console.log('Request completed');
        
        // Hide status after delay
        setTimeout(() => hideStatus(), 3000);
    }
}

function addMessage(sender, content, sources = [], visualizations = []) {
    const messagesDiv = document.getElementById('chatMessages');
    
    // Clear welcome message
    if (messageCount === 0) {
        messagesDiv.innerHTML = '';
    }
    messageCount++;

    const messageDiv = document.createElement('div');
    messageDiv.className = 'message ' + sender;

    let sourcesHtml = '';
    if (sources && sources.length > 0) {
        sourcesHtml = `
            <div class="sources">
                Sources: ${sources.map(s => `<span>${s}</span>`).join('')}
            </div>
        `;
    }

    let visualizationHtml = '';
    if (visualizations && visualizations.length > 0) {
        console.log('Processing visualizations:', visualizations.length);
        visualizationHtml = visualizations.map((viz, index) => {
            console.log(`Visualization ${index}:`, viz.substring(0, 100));
            return `<div class="visualization-container" id="viz-${Date.now()}-${index}">${viz}</div>`;
        }).join('');
    }

    // Format content based on sender
    let formattedContent = content;
    if (sender === 'assistant') {
        // Convert markdown to HTML for assistant responses
        try {
            formattedContent = marked.parse(content);
        } catch (error) {
            // Fallback to basic formatting if marked.js fails
            console.warn('Markdown parsing failed, using fallback:', error);
            formattedContent = content
                .replace(/\n/g, '<br>')
                .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
                .replace(/\*(.*?)\*/g, '<em>$1</em>')
                .replace(/`(.*?)`/g, '<code>$1</code>');
        }
    } else {
        // Apply markdown parsing to user messages too
        try {
            formattedContent = marked.parse(content);
        } catch (error) {
            formattedContent = content.replace(/\n/g, '<br>');
        }
    }

    messageDiv.innerHTML = `
        <div class="message-content">
            ${formattedContent}
            ${sourcesHtml}
        </div>
        ${visualizationHtml}
        <div class="message-meta">${new Date().toLocaleTimeString()}</div>
    `;

    messagesDiv.appendChild(messageDiv);
    messagesDiv.scrollTop = messagesDiv.scrollHeight;

    // Execute any scripts in the visualizations after DOM insertion
    if (visualizations && visualizations.length > 0) {
        console.log('Executing visualization scripts...');
        setTimeout(() => {
            const scripts = messageDiv.querySelectorAll('script');
            console.log(`Found ${scripts.length} scripts to execute`);
            
            scripts.forEach((script, index) => {
                console.log(`Executing script ${index}:`, script.textContent.substring(0, 200) + '...');
                try {
                    // Execute script in global context using Function constructor
                    const scriptFunction = new Function(script.textContent);
                    scriptFunction.call(window);
                    console.log(`Script ${index} executed successfully`);
                } catch (error) {
                    console.error(`Script ${index} execution error:`, error);
                    console.error(`Script content preview:`, script.textContent.substring(0, 500));
                }
            });
            console.log('All visualization scripts executed');
        }, 100);
    }

    chatHistory.push({ role: sender, content });
    if (chatHistory.length > 20) chatHistory = chatHistory.slice(-20);
}

function setQuery(query) {
    document.getElementById('queryInput').value = query;
    setTimeout(() => sendQuery(), 100);
}

// Status management functions
function showStatus(message, type = 'info') {
    const statusIndicator = document.getElementById('statusIndicator');
    const statusText = document.getElementById('statusText');
    
    statusText.textContent = message;
    statusIndicator.className = `status-indicator show ${type}`;
}

function hideStatus() {
    const statusIndicator = document.getElementById('statusIndicator');
    statusIndicator.classList.remove('show');
}

function updateProgress(progress) {
    // Update progress bar if it exists
    const progressBar = document.querySelector('.progress-bar');
    if (progressBar) {
        progressBar.style.width = `${progress}%`;
    }
    
    // Update loading indicator text with progress
    const loadingText = document.getElementById('loadingText');
    if (loadingText && progress) {
        loadingText.textContent = `Processing ${progress}%...`;
    }
}

// Theme toggle functionality
function toggleTheme() {
    const currentTheme = document.documentElement.getAttribute('data-theme');
    const newTheme = currentTheme === 'light' ? 'dark' : 'light';
    const themeIcon = document.querySelector('#themeToggle i');
    
    document.documentElement.setAttribute('data-theme', newTheme);
    localStorage.setItem('theme', newTheme);
    
    // Update icon
    if (newTheme === 'light') {
        themeIcon.className = 'fas fa-sun';
    } else {
        themeIcon.className = 'fas fa-moon';
    }
}

// Initialize theme
function initializeTheme() {
    const savedTheme = localStorage.getItem('theme') || 'dark';
    const themeIcon = document.querySelector('#themeToggle i');
    
    document.documentElement.setAttribute('data-theme', savedTheme);
    
    if (savedTheme === 'light') {
        themeIcon.className = 'fas fa-sun';
    } else {
        themeIcon.className = 'fas fa-moon';
    }
}

// Event listeners
document.getElementById('queryInput').addEventListener('keypress', (e) => {
    if (e.key === 'Enter') sendQuery();
});

document.getElementById('sendBtn').addEventListener('click', (e) => {
    console.log('Research button clicked');
    e.preventDefault();
    sendQuery();
});

document.getElementById('themeToggle').addEventListener('click', toggleTheme);

// Initialize
document.addEventListener('DOMContentLoaded', () => {
    console.log('Application initialized');
    initializeTheme();
    checkStatus();
    document.getElementById('queryInput').focus();
});