Spaces:
Running
Running
| document.addEventListener("DOMContentLoaded", () => { | |
| const workerUrl = "https://ctmresearchagent.aiagents.workers.dev"; // Replace if your URL is different | |
| // Event Listeners | |
| document.getElementById("uploadForm").addEventListener("submit", handleFileUpload); | |
| document.getElementById("submitButton").addEventListener("click", handleAiQuerySubmit); | |
| // Initial data load | |
| loadReportsFromRss(); | |
| loadResearchHistory(); | |
| /** | |
| * Fetches and displays the list of reports from the backend's RSS parser | |
| */ | |
| async function loadReportsFromRss() { | |
| const reportsList = document.getElementById("reportsList"); | |
| reportsList.innerHTML = "<p>Fetching reports...</p>"; | |
| try { | |
| const response = await fetch(`${workerUrl}/api/rss`); | |
| if (!response.ok) throw new Error('Failed to fetch RSS feed.'); | |
| const items = await response.json(); | |
| reportsList.innerHTML = ""; | |
| if (items.length === 0) { reportsList.innerHTML = "<p>No reports found in the feed.</p>"; return; } | |
| items.forEach(item => { | |
| const entryDiv = document.createElement("div"); | |
| entryDiv.className = "report-entry"; | |
| entryDiv.innerHTML = `<a href="${item.link}" target="_blank" rel="noopener noreferrer">${item.title}</a><p>${item.description}</p>`; | |
| reportsList.appendChild(entryDiv); | |
| }); | |
| } catch (error) { | |
| reportsList.innerHTML = `<p>Could not load reports: ${error.message}</p>`; | |
| } | |
| } | |
| // Simplified File Upload | |
| async function handleFileUpload(event) { | |
| event.preventDefault(); | |
| const fileInput = document.getElementById("fileInput"); | |
| const uploadStatus = document.getElementById("uploadStatus"); | |
| const file = fileInput.files[0]; | |
| if (!file) { uploadStatus.textContent = 'Please select a file.'; return; } | |
| uploadStatus.textContent = `Uploading ${file.name}...`; | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| try { | |
| const response = await fetch(`${workerUrl}/api/upload`, { method: 'POST', body: formData }); | |
| const data = await response.json(); | |
| if (!response.ok) throw new Error(data.error || 'Upload failed'); | |
| uploadStatus.textContent = `Success: ${data.message}`; | |
| fileInput.value = ''; // Clear the file input | |
| } catch (error) { | |
| uploadStatus.textContent = `Error: ${error.message}`; | |
| } | |
| } | |
| // Simplified AI Query | |
| async function handleAiQuerySubmit() { | |
| const researchQueryInput = document.getElementById("researchQuery"); | |
| const resultDisplay = document.getElementById("resultDisplay"); | |
| const submitButton = document.getElementById("submitButton"); | |
| const query = researchQueryInput.value.trim(); | |
| if (!query) { alert("Please enter a query."); return; } | |
| submitButton.disabled = true; | |
| submitButton.innerText = "Thinking..."; | |
| resultDisplay.innerHTML = "<p>Generating response...</p>"; | |
| try { | |
| const response = await fetch(`${workerUrl}/api/query`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ query: query }) }); | |
| const data = await response.json(); | |
| if (!response.ok) throw new Error(data.error || "An unknown error occurred."); | |
| resultDisplay.innerText = data.result; | |
| await loadResearchHistory(); // Refresh history after a successful query | |
| } catch (error) { | |
| resultDisplay.innerText = `Error: ${error.message}`; | |
| } finally { | |
| submitButton.disabled = false; | |
| submitButton.innerText = "Submit"; | |
| } | |
| } | |
| // Unchanged History Loader | |
| async function loadResearchHistory() { | |
| const historyContainer = document.getElementById("historyContainer"); | |
| try { | |
| const response = await fetch(`${workerUrl}/api/research`); | |
| if (!response.ok) throw new Error('Failed to load history.'); | |
| const logs = await response.json(); | |
| historyContainer.innerHTML = ""; | |
| if (logs.length === 0) { historyContainer.innerHTML = "<p>No research history found.</p>"; return; } | |
| logs.forEach(log => { | |
| const entryDiv = document.createElement("div"); | |
| entryDiv.className = "history-entry"; | |
| entryDiv.innerHTML = `<p><strong>Query:</strong> ${log.query}</p><p><strong>Result:</strong> ${log.result}</p><p><small><em>${new Date(log.timestamp).toLocaleString()}</em></small></p>`; | |
| historyContainer.appendChild(entryDiv); | |
| }); | |
| } catch (error) { | |
| historyContainer.innerHTML = "<p>Error loading research history.</p>"; | |
| } | |
| } | |
| }); |