File size: 15,888 Bytes
ce2f3a9
 
d833ce9
4f6532d
d833ce9
 
 
 
ce2f3a9
d833ce9
ce2f3a9
 
 
4f6532d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce2f3a9
 
4f6532d
 
 
 
 
 
 
 
 
 
 
 
 
d833ce9
4f6532d
ce2f3a9
4f6532d
 
 
 
 
ce2f3a9
4f6532d
 
 
 
 
 
 
 
ce2f3a9
4f6532d
 
 
ce2f3a9
d833ce9
 
 
 
 
 
4f6532d
 
 
d833ce9
4f6532d
 
 
d833ce9
4f6532d
 
 
 
 
ce2f3a9
4f6532d
 
 
 
 
 
 
 
 
 
ce2f3a9
 
 
4f6532d
 
 
 
 
 
 
 
 
 
 
d833ce9
 
4f6532d
 
 
d833ce9
 
 
 
4f6532d
 
 
 
 
 
 
 
 
 
 
 
ce2f3a9
 
4f6532d
 
 
 
 
ce2f3a9
4f6532d
 
 
ce2f3a9
 
4f6532d
ce2f3a9
4f6532d
 
d833ce9
4f6532d
d833ce9
 
ce2f3a9
 
d833ce9
 
ce2f3a9
4f6532d
ce2f3a9
4f6532d
d833ce9
 
4f6532d
 
 
d833ce9
 
 
4f6532d
ce2f3a9
4f6532d
d833ce9
ce2f3a9
4f6532d
 
d833ce9
ce2f3a9
 
 
4f6532d
 
 
 
 
ce2f3a9
 
d833ce9
ce2f3a9
d833ce9
ce2f3a9
9121aae
 
 
 
d833ce9
4f6532d
 
 
ce2f3a9
4f6532d
9121aae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d833ce9
ce2f3a9
4f6532d
ce2f3a9
 
 
d833ce9
 
ce2f3a9
4f6532d
 
ce2f3a9
4f6532d
 
 
ce2f3a9
 
 
4f6532d
 
 
ce2f3a9
d833ce9
4f6532d
d833ce9
 
ce2f3a9
d833ce9
ce2f3a9
d833ce9
 
ce2f3a9
 
4f6532d
 
d833ce9
 
4f6532d
 
 
 
d833ce9
 
 
 
 
9121aae
 
 
 
d833ce9
4f6532d
 
 
 
d833ce9
4f6532d
d833ce9
4f6532d
 
 
9121aae
 
 
 
 
4f6532d
 
9121aae
4f6532d
 
 
 
 
 
9121aae
4f6532d
 
 
 
ce2f3a9
d833ce9
 
ce2f3a9
d833ce9
 
 
 
 
 
 
 
 
ce2f3a9
d833ce9
 
 
4f6532d
d833ce9
ce2f3a9
 
 
9121aae
 
ce2f3a9
 
9121aae
d833ce9
4f6532d
 
d833ce9
 
4f6532d
ce2f3a9
4f6532d
 
 
 
ce2f3a9
 
 
d833ce9
4f6532d
 
ce2f3a9
 
d833ce9
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
/**
 * kb-controller.js β€” Knowledge Base Management Controller
 * =========================================================
 * Handles specialized navigation and multi-file upload for policy documents.
 * * Hardened for Enterprise MVC:
 * - Integrates globally with `apiFetch` for automatic CSRF & HTTP-Only cookies.
 * - Utilises `SecurityUtils` for XSS immunity on dynamic DOM generation.
 * - Enforces Vercel Serverless batch constraints (max 5 files).
 */

(function () {
    'use strict';

    // ── Elements & State ───────────────────────────────────────────────────
    const kbTabs = document.querySelectorAll('.nav-tab');
    const kbPanels = document.querySelectorAll('.content-panel');
    const kbDropzone = document.getElementById('kb-dropzone');
    const kbFileInput = document.getElementById('pdf-input');
    const kbChipsContainer = document.getElementById('kb-file-chips');
    const kbUploadBtn = document.getElementById('process-upload-btn');
    const kbLoader = document.getElementById('loader');
    const kbLoaderText = document.getElementById('loader-text');
    const kbEmptyState = document.getElementById('empty-state');
    const kbDocsGrid = document.getElementById('documents-grid');
    const kbDocCount = document.getElementById('doc-count');

    let queuedFiles = [];
    let pollingInterval = null;

    console.info('[KBController] Initializing Knowledge Base specialized controller...');

    // ── Tabs Logic ──────────────────────────────────────────────────────────
    function initTabs() {
        if (!kbTabs.length) return;
        
        kbTabs.forEach(tab => {
            tab.addEventListener('click', (e) => {
                const targetId = tab.getAttribute('aria-controls');
                const targetPanel = document.getElementById(targetId);

                if (!targetPanel) return;

                // Toggle Active States
                kbTabs.forEach(t => {
                    t.classList.remove('active');
                    t.setAttribute('aria-selected', 'false');
                });
                kbPanels.forEach(p => p.classList.remove('active'));

                tab.classList.add('active');
                tab.setAttribute('aria-selected', 'true');
                targetPanel.classList.add('active');

                // Trigger actions
                if (tab.dataset.tab === 'documents') {
                    loadDocuments();
                } else {
                    stopPolling();
                }
            });
        });
    }

    // ── File Selection & Chips ──────────────────────────────────────────────
    function initFileHandlers() {
        if (kbDropzone) {
            kbDropzone.addEventListener('dragover', e => {
                e.preventDefault();
                kbDropzone.classList.add('drag-over');
            });
            kbDropzone.addEventListener('dragleave', () => kbDropzone.classList.remove('drag-over'));
            kbDropzone.addEventListener('drop', e => {
                e.preventDefault();
                kbDropzone.classList.remove('drag-over');
                handleFileSelect(Array.from(e.dataTransfer.files));
            });

        }

        if (kbFileInput) {
            kbFileInput.addEventListener('change', e => {
                handleFileSelect(Array.from(e.target.files));
            });
        }

        if (kbUploadBtn) {
            kbUploadBtn.addEventListener('click', () => {
                if (queuedFiles.length > 0) {
                    uploadDocuments(queuedFiles);
                }
            });
        }
    }

    function handleFileSelect(files) {
        if (!files || files.length === 0) return;
        const allowedExtensions = ['.pdf', '.doc', '.docx', '.txt', '.csv'];

        // Vercel Timeout Guard Enforcement (Max 5 files per batch)
        if (queuedFiles.length + files.length > 5) {
            SecurityUtils.showToast('Maximum 5 files allowed per upload batch to prevent server timeouts.', 'warning');
            files = files.slice(0, Math.max(0, 5 - queuedFiles.length));
        }

        for (const file of files) {
            const ext = file.name.substring(file.name.lastIndexOf('.')).toLowerCase();
            if (!allowedExtensions.includes(ext)) {
                SecurityUtils.showToast(`Unsupported format: ${file.name}`, 'error');
                continue;
            }
            if (file.size > 50 * 1024 * 1024) {
                SecurityUtils.showToast(`File too large (Max 50MB): ${file.name}`, 'error');
                continue;
            }
            if (!queuedFiles.some(f => f.name === file.name && f.size === file.size)) {
                queuedFiles.push(file);
            }
        }
        renderFileChips();
        updateUploadButton();
    }

    function renderFileChips() {
        if (!kbChipsContainer) return;
        kbChipsContainer.textContent = '';
        
        if (queuedFiles.length === 0) {
            kbChipsContainer.style.display = 'none';
            return;
        }

        kbChipsContainer.style.display = 'flex';
        queuedFiles.forEach((file, index) => {
            const chip = document.createElement('div');
            chip.className = 'file-chip';
            
            const icon = document.createElement('span');
            icon.className = 'material-symbols-rounded';
            const ext = file.name.substring(file.name.lastIndexOf('.')).toLowerCase();
            icon.textContent = (ext === '.pdf') ? 'picture_as_pdf' : (ext === '.csv') ? 'table_chart' : 'description';
            
            const name = document.createElement('span');
            name.className = 'chip-name';
            name.textContent = file.name; 
            
            const removeBtn = document.createElement('button');
            removeBtn.className = 'chip-remove';
            removeBtn.type = 'button';
            removeBtn.innerHTML = '<span class="material-symbols-rounded" style="font-size:1.1rem">close</span>';
            removeBtn.title = 'Remove file';
            
            removeBtn.addEventListener('click', (e) => {
                e.stopPropagation();
                queuedFiles.splice(index, 1);
                renderFileChips();
                updateUploadButton();
            });

            chip.appendChild(icon);
            chip.appendChild(name);
            chip.appendChild(removeBtn);
            kbChipsContainer.appendChild(chip);
        });
    }

    function updateUploadButton() {
        if (kbUploadBtn) {
            kbUploadBtn.disabled = (queuedFiles.length === 0);
        }
    }

    // ── API Operations ──────────────────────────────────────────────────────
    async function uploadDocuments(files) {
        if (!files.length) return;

        const formData = new FormData();
        files.forEach(file => formData.append('file', file));

        if (kbLoader) {
            kbLoader.hidden = false;
            if (kbLoaderText) kbLoaderText.textContent = `Uploading & Indexing ${files.length} document(s)...`;
        }
        
        SecurityUtils.setButtonLoading(kbUploadBtn, true);

        try {
            // Use global apiFetch for automatic CSRF injection and HTTP-Only cookie forwarding
            const data = await apiFetch('/kb/upload', {
                method: 'POST',
                body: formData
            });

            if (data.success) {
                SecurityUtils.showToast(`Successfully queued ${data.results.length} document(s) for indexing`, 'success');
                queuedFiles = [];
                renderFileChips();
                updateUploadButton();
                
                // Automatically Switch to Manage tab to view indexing progress
                const docTab = document.querySelector('[data-tab="documents"]');
                if (docTab) docTab.click();
            }
        } catch (err) {
            ErrorHandler.showError(err);
        } finally {
            if (kbLoader) kbLoader.hidden = true;
            if (kbFileInput) kbFileInput.value = '';
            SecurityUtils.setButtonLoading(kbUploadBtn, false);
        }
    }

    async function loadDocuments(quiet = false) {
        if (!quiet && kbLoader) {
            kbLoader.hidden = false;
            if (kbLoaderText) kbLoaderText.textContent = 'Loading policy library...';
        }

        try {
            const data = await apiFetch('/kb/documents');

            if (data.success) {
                renderDocuments(data.documents);
                // Cache documents locally for offline viewing
                try {
                    localStorage.setItem('qualora_kb_docs_v1', JSON.stringify(data.documents));
                } catch (e) { /* ignore storage errors */ }
                // Check if any docs are actively indexing to start/stop polling
                const isIndexing = data.documents.some(d => ['indexing', 'pending'].includes(d.status));
                if (isIndexing) startPolling();
                else stopPolling();
            }
        } catch (err) {
            console.error('[KB] loadDocuments failed:', err);
            // Fallback to cached documents in localStorage
            try {
                const cached = localStorage.getItem('qualora_kb_docs_v1');
                if (cached) {
                    const docs = JSON.parse(cached);
                    renderDocuments(docs);
                    if (window.SecurityUtils && !quiet) window.SecurityUtils.showToast('Using cached KB documents (offline)', 'info');
                    // If any doc shows indexing, start polling to try refreshing status
                    const isIndexing = docs.some(d => ['indexing', 'pending'].includes(d.status));
                    if (isIndexing) startPolling();
                    else stopPolling();
                    return;
                }
            } catch (cacheErr) {
                console.error('[KB] cached docs parse failed:', cacheErr);
            }
            if (!quiet) ErrorHandler.showError(err);
        } finally {
            if (!quiet && kbLoader) kbLoader.hidden = true;
        }
    }

    // ── UI Renderers ────────────────────────────────────────────────────────
    
    function renderDocuments(documents) {
        if (!kbDocsGrid || !kbEmptyState) return;

        if (!documents || documents.length === 0) {
            kbEmptyState.hidden = false;
            kbDocsGrid.hidden = true;
            if (kbDocCount) kbDocCount.textContent = '0 documents';
            return;
        }

        kbEmptyState.hidden = true;
        kbDocsGrid.hidden = false;
        if (kbDocCount) kbDocCount.textContent = `${documents.length} document${documents.length !== 1 ? 's' : ''}`;

        // Clear existing content and use fragment for performance
        kbDocsGrid.textContent = '';
        const fragment = document.createDocumentFragment();
        
        documents.forEach(doc => {
            fragment.appendChild(createDocCard(doc));
        });
        
        kbDocsGrid.appendChild(fragment);
    }

    function createDocCard(doc) {
        const card = document.createElement('div');
        const safeStatus = SecurityUtils.escapeHTML(doc.status);
        card.className = `doc-card ${safeStatus}`;
        
        const ext = doc.filename.substring(doc.filename.lastIndexOf('.')).toLowerCase();
        const iconName = (ext === '.pdf') ? 'picture_as_pdf' : (ext === '.csv') ? 'table_chart' : 'description';

        // XSS Safeguard: Encode values before HTML injection
        const safeName = SecurityUtils.escapeHTML(doc.filename);
        const safeDate = doc.created_at ? SecurityUtils.escapeHTML(new Date(doc.created_at).toLocaleDateString()) : 'Unknown';
        const chunkCount = SecurityUtils.escapeHTML(doc.chunk_count || 0);
        const displayStatus = safeStatus.charAt(0).toUpperCase() + safeStatus.slice(1);
        const atlasIndexed = Boolean(doc.vector_indexed);
        const chromaIndexed = Boolean(doc.chroma_indexed);
        const atlasStatusText = atlasIndexed ? 'βœ… Indexed' : '⏳ Pending';
        const chromaStatusText = chromaIndexed ? 'βœ… Indexed' : '⏳ Pending';

        card.innerHTML = `
            <div class="doc-card-header">
                <span class="material-symbols-rounded doc-icon">${iconName}</span>
                <div class="doc-info">
                    <h4 class="doc-name">${safeName}</h4>
                    <div class="doc-meta">
                        <span>${safeDate}</span>
                    </div>
                </div>
            </div>
            <div class="doc-status-details">
                <p><strong>Status:</strong> ${displayStatus}</p>
                <p><strong>Total Vector Chunks:</strong> ${chunkCount}</p>
                <p><strong>Atlas DB (Primary):</strong> ${atlasStatusText}</p>
                <p><strong>ChromaDB (Local):</strong> ${chromaStatusText}</p>
            </div>
            <div class="doc-actions">
                <button class="doc-action-btn view-btn" data-id="${doc._id}">view</button>
                <button class="doc-action-btn delete-btn" data-id="${doc._id}" title="Delete">
                    <span class="material-symbols-rounded" style="font-size:1.1rem">delete</span>
                </button>
            </div>
        `;

        card.querySelector('.view-btn').addEventListener('click', () => viewDocumentFile(doc._id));
        card.querySelector('.delete-btn').addEventListener('click', () => deleteDocument(doc._id));

        return card;
    }

    // ── Action Handlers ─────────────────────────────────────────────────────
    
    async function deleteDocument(docId) {
        const confirmed = await SecurityUtils.showConfirmDialog(
            'Delete Document', 
            'Permanently delete this policy document? All vector index chunks will be removed.', 
            'Delete', 
            'Cancel'
        );
        
        if (!confirmed) return;
        
        try {
            await apiFetch(`/kb/${docId}`, { method: 'DELETE' });
            SecurityUtils.showToast('Document deleted successfully', 'success');
            loadDocuments();
        } catch (err) {
            ErrorHandler.showError(err);
        }
    }

    function viewDocumentFile(docId) {
        window.open(`/api/kb/${encodeURIComponent(docId)}/file`, '_blank', 'noopener,noreferrer');
    }

    // ── Polling ─────────────────────────────────────────────────────────────
    
    function startPolling() {
        if (pollingInterval) return;
        // 5s polling interval keeps backend CPU strain minimal
        pollingInterval = setInterval(() => loadDocuments(true), 5000);
    }

    function stopPolling() {
        if (pollingInterval) {
            clearInterval(pollingInterval);
            pollingInterval = null;
        }
    }

    // ── Initialization ──────────────────────────────────────────────────────────
    initTabs();
    initFileHandlers();
    loadDocuments();

})();