Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,58 +1,54 @@
|
|
| 1 |
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
-
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev";
|
| 3 |
|
| 4 |
// Event Listeners
|
| 5 |
document.getElementById("uploadForm").addEventListener("submit", handleReportUpload);
|
| 6 |
document.getElementById("submitButton").addEventListener("click", handleAiQuerySubmit);
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
// Initial data load
|
| 9 |
loadReportsList();
|
| 10 |
loadResearchHistory();
|
| 11 |
|
| 12 |
/**
|
| 13 |
-
* NEW:
|
| 14 |
*/
|
| 15 |
-
async function
|
| 16 |
-
event.
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
description: document.getElementById('reportDescription').value,
|
| 22 |
-
body: document.getElementById('reportBody').value,
|
| 23 |
-
};
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
if (!response.ok) throw new Error(data.error || 'Failed to process report.');
|
| 42 |
-
|
| 43 |
-
uploadStatus.textContent = `Success: ${data.message}`;
|
| 44 |
-
document.getElementById("uploadForm").reset();
|
| 45 |
-
await loadReportsList(); // Refresh the list
|
| 46 |
-
} catch (error) {
|
| 47 |
-
uploadStatus.textContent = `Error: ${error.message}`;
|
| 48 |
-
} finally {
|
| 49 |
-
uploadButton.disabled = false;
|
| 50 |
-
uploadButton.innerText = "Process and Add Report";
|
| 51 |
}
|
| 52 |
}
|
| 53 |
-
|
| 54 |
/**
|
| 55 |
-
*
|
| 56 |
*/
|
| 57 |
async function loadReportsList() {
|
| 58 |
const reportsList = document.getElementById("reportsList");
|
|
@@ -68,7 +64,14 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 68 |
documents.forEach(doc => {
|
| 69 |
const entryDiv = document.createElement("div");
|
| 70 |
entryDiv.className = "report-entry";
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
reportsList.appendChild(entryDiv);
|
| 73 |
});
|
| 74 |
} catch (error) {
|
|
@@ -76,49 +79,8 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
-
//
|
| 80 |
-
async function
|
| 81 |
-
async function
|
| 82 |
-
|
| 83 |
-
// Paste the unchanged functions here
|
| 84 |
-
async function handleAiQuerySubmit() {
|
| 85 |
-
const researchQueryInput = document.getElementById("researchQuery");
|
| 86 |
-
const resultDisplay = document.getElementById("resultDisplay");
|
| 87 |
-
const submitButton = document.getElementById("submitButton");
|
| 88 |
-
const query = researchQueryInput.value.trim();
|
| 89 |
-
if (!query) { alert("Please enter a query."); return; }
|
| 90 |
-
submitButton.disabled = true;
|
| 91 |
-
submitButton.innerText = "Thinking...";
|
| 92 |
-
resultDisplay.innerHTML = "<p>Generating response...</p>";
|
| 93 |
-
try {
|
| 94 |
-
const response = await fetch(`${workerUrl}/api/query`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ query: query }) });
|
| 95 |
-
const data = await response.json();
|
| 96 |
-
if (!response.ok) throw new Error(data.error || "An unknown error occurred.");
|
| 97 |
-
resultDisplay.innerText = data.result;
|
| 98 |
-
await loadResearchHistory();
|
| 99 |
-
} catch (error) {
|
| 100 |
-
resultDisplay.innerText = `Error: ${error.message}`;
|
| 101 |
-
} finally {
|
| 102 |
-
submitButton.disabled = false;
|
| 103 |
-
submitButton.innerText = "Submit";
|
| 104 |
-
}
|
| 105 |
-
}
|
| 106 |
-
async function loadResearchHistory() {
|
| 107 |
-
const historyContainer = document.getElementById("historyContainer");
|
| 108 |
-
try {
|
| 109 |
-
const response = await fetch(`${workerUrl}/api/research`);
|
| 110 |
-
if (!response.ok) throw new Error('Failed to load history.');
|
| 111 |
-
const logs = await response.json();
|
| 112 |
-
historyContainer.innerHTML = "";
|
| 113 |
-
if (logs.length === 0) { historyContainer.innerHTML = "<p>No research history found.</p>"; return; }
|
| 114 |
-
logs.forEach(log => {
|
| 115 |
-
const entryDiv = document.createElement("div");
|
| 116 |
-
entryDiv.className = "history-entry";
|
| 117 |
-
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>`;
|
| 118 |
-
historyContainer.appendChild(entryDiv);
|
| 119 |
-
});
|
| 120 |
-
} catch (error) {
|
| 121 |
-
historyContainer.innerHTML = "<p>Error loading research history.</p>";
|
| 122 |
-
}
|
| 123 |
-
}
|
| 124 |
});
|
|
|
|
| 1 |
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
+
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev";
|
| 3 |
|
| 4 |
// Event Listeners
|
| 5 |
document.getElementById("uploadForm").addEventListener("submit", handleReportUpload);
|
| 6 |
document.getElementById("submitButton").addEventListener("click", handleAiQuerySubmit);
|
| 7 |
|
| 8 |
+
// NEW: Event listener for opening the modal
|
| 9 |
+
document.getElementById("reportsList").addEventListener("click", handleViewReportClick);
|
| 10 |
+
|
| 11 |
+
// NEW: Event listeners for closing the modal
|
| 12 |
+
const modal = document.getElementById('reportModal');
|
| 13 |
+
document.querySelector('.modal-close-button').addEventListener('click', () => { modal.style.display = 'none'; });
|
| 14 |
+
window.addEventListener('click', (event) => { if (event.target == modal) { modal.style.display = 'none'; }});
|
| 15 |
+
|
| 16 |
// Initial data load
|
| 17 |
loadReportsList();
|
| 18 |
loadResearchHistory();
|
| 19 |
|
| 20 |
/**
|
| 21 |
+
* NEW: Checks if a "View" button was clicked and opens the report
|
| 22 |
*/
|
| 23 |
+
async function handleViewReportClick(event) {
|
| 24 |
+
if (event.target.classList.contains('view-report-btn')) {
|
| 25 |
+
const docId = event.target.dataset.id;
|
| 26 |
+
const modal = document.getElementById('reportModal');
|
| 27 |
+
const modalTitle = document.getElementById('modalTitle');
|
| 28 |
+
const modalBody = document.getElementById('modalBody');
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
// Show loading state
|
| 31 |
+
modalTitle.textContent = "Loading Report...";
|
| 32 |
+
modalBody.textContent = "";
|
| 33 |
+
modal.style.display = 'block';
|
| 34 |
|
| 35 |
+
try {
|
| 36 |
+
const response = await fetch(`${workerUrl}/api/document/${docId}`);
|
| 37 |
+
if (!response.ok) throw new Error('Document not found.');
|
| 38 |
+
const doc = await response.json();
|
| 39 |
|
| 40 |
+
modalTitle.textContent = doc.title;
|
| 41 |
+
modalBody.textContent = doc.body;
|
| 42 |
+
|
| 43 |
+
} catch(error) {
|
| 44 |
+
modalTitle.textContent = "Error";
|
| 45 |
+
modalBody.textContent = error.message;
|
| 46 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
}
|
| 49 |
+
|
| 50 |
/**
|
| 51 |
+
* REVISED: Adds a "View" button for each report
|
| 52 |
*/
|
| 53 |
async function loadReportsList() {
|
| 54 |
const reportsList = document.getElementById("reportsList");
|
|
|
|
| 64 |
documents.forEach(doc => {
|
| 65 |
const entryDiv = document.createElement("div");
|
| 66 |
entryDiv.className = "report-entry";
|
| 67 |
+
// Add the data-id attribute to the button
|
| 68 |
+
entryDiv.innerHTML = `
|
| 69 |
+
<div>
|
| 70 |
+
<h3>${doc.title}</h3>
|
| 71 |
+
<p>${doc.description}</p>
|
| 72 |
+
</div>
|
| 73 |
+
<button class="view-report-btn" data-id="${doc.id}">View</button>
|
| 74 |
+
`;
|
| 75 |
reportsList.appendChild(entryDiv);
|
| 76 |
});
|
| 77 |
} catch (error) {
|
|
|
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
| 82 |
+
// --- All other functions (handleReportUpload, handleAiQuerySubmit, loadResearchHistory) are unchanged ---
|
| 83 |
+
async function handleReportUpload(event) { event.preventDefault(); const uploadButton = document.getElementById("uploadButton"); const uploadStatus = document.getElementById("uploadStatus"); const report = { title: document.getElementById('reportTitle').value, description: document.getElementById('reportDescription').value, body: document.getElementById('reportBody').value, }; if (!report.title || !report.description || !report.body) { uploadStatus.textContent = 'Please fill out all fields.'; return; } uploadButton.disabled = true; uploadButton.innerText = "Processing..."; uploadStatus.textContent = "Submitting report..."; try { const response = await fetch(`${workerUrl}/api/upload`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(report), }); const data = await response.json(); if (!response.ok) throw new Error(data.error || 'Failed to process report.'); uploadStatus.textContent = `Success: ${data.message}`; document.getElementById("uploadForm").reset(); await loadReportsList(); } catch (error) { uploadStatus.textContent = `Error: ${error.message}`; } finally { uploadButton.disabled = false; uploadButton.innerText = "Process and Add Report"; } }
|
| 84 |
+
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(); } catch (error) { resultDisplay.innerText = `Error: ${error.message}`; } finally { submitButton.disabled = false; submitButton.innerText = "Submit"; } }
|
| 85 |
+
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>"; } }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
});
|