Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,80 +1,101 @@
|
|
| 1 |
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
-
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev"; //
|
| 3 |
|
| 4 |
// Event Listeners
|
| 5 |
-
document.getElementById("uploadForm").addEventListener("submit",
|
| 6 |
document.getElementById("submitButton").addEventListener("click", handleAiQuerySubmit);
|
| 7 |
|
| 8 |
// Initial data load
|
| 9 |
-
|
| 10 |
loadResearchHistory();
|
| 11 |
|
| 12 |
/**
|
| 13 |
-
*
|
| 14 |
*/
|
| 15 |
-
async function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
const reportsList = document.getElementById("reportsList");
|
| 17 |
reportsList.innerHTML = "<p>Fetching reports...</p>";
|
| 18 |
try {
|
| 19 |
-
const response = await fetch(`${workerUrl}/api/
|
| 20 |
-
if (!response.ok) throw new Error('Failed to fetch
|
| 21 |
-
const
|
| 22 |
|
| 23 |
reportsList.innerHTML = "";
|
| 24 |
-
if (
|
| 25 |
|
| 26 |
-
|
| 27 |
const entryDiv = document.createElement("div");
|
| 28 |
entryDiv.className = "report-entry";
|
| 29 |
-
entryDiv.innerHTML = `<
|
| 30 |
reportsList.appendChild(entryDiv);
|
| 31 |
});
|
| 32 |
} catch (error) {
|
| 33 |
reportsList.innerHTML = `<p>Could not load reports: ${error.message}</p>`;
|
| 34 |
}
|
| 35 |
}
|
| 36 |
-
|
| 37 |
-
// Simplified File Upload
|
| 38 |
-
async function handleFileUpload(event) {
|
| 39 |
-
event.preventDefault();
|
| 40 |
-
const fileInput = document.getElementById("fileInput");
|
| 41 |
-
const uploadStatus = document.getElementById("uploadStatus");
|
| 42 |
-
const file = fileInput.files[0];
|
| 43 |
-
if (!file) { uploadStatus.textContent = 'Please select a file.'; return; }
|
| 44 |
-
|
| 45 |
-
uploadStatus.textContent = `Uploading ${file.name}...`;
|
| 46 |
-
const formData = new FormData();
|
| 47 |
-
formData.append('file', file);
|
| 48 |
-
|
| 49 |
-
try {
|
| 50 |
-
const response = await fetch(`${workerUrl}/api/upload`, { method: 'POST', body: formData });
|
| 51 |
-
const data = await response.json();
|
| 52 |
-
if (!response.ok) throw new Error(data.error || 'Upload failed');
|
| 53 |
-
uploadStatus.textContent = `Success: ${data.message}`;
|
| 54 |
-
fileInput.value = ''; // Clear the file input
|
| 55 |
-
} catch (error) {
|
| 56 |
-
uploadStatus.textContent = `Error: ${error.message}`;
|
| 57 |
-
}
|
| 58 |
-
}
|
| 59 |
|
| 60 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
async function handleAiQuerySubmit() {
|
| 62 |
const researchQueryInput = document.getElementById("researchQuery");
|
| 63 |
const resultDisplay = document.getElementById("resultDisplay");
|
| 64 |
const submitButton = document.getElementById("submitButton");
|
| 65 |
const query = researchQueryInput.value.trim();
|
| 66 |
if (!query) { alert("Please enter a query."); return; }
|
| 67 |
-
|
| 68 |
submitButton.disabled = true;
|
| 69 |
submitButton.innerText = "Thinking...";
|
| 70 |
resultDisplay.innerHTML = "<p>Generating response...</p>";
|
| 71 |
-
|
| 72 |
try {
|
| 73 |
const response = await fetch(`${workerUrl}/api/query`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ query: query }) });
|
| 74 |
const data = await response.json();
|
| 75 |
if (!response.ok) throw new Error(data.error || "An unknown error occurred.");
|
| 76 |
resultDisplay.innerText = data.result;
|
| 77 |
-
await loadResearchHistory();
|
| 78 |
} catch (error) {
|
| 79 |
resultDisplay.innerText = `Error: ${error.message}`;
|
| 80 |
} finally {
|
|
@@ -82,8 +103,6 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 82 |
submitButton.innerText = "Submit";
|
| 83 |
}
|
| 84 |
}
|
| 85 |
-
|
| 86 |
-
// Unchanged History Loader
|
| 87 |
async function loadResearchHistory() {
|
| 88 |
const historyContainer = document.getElementById("historyContainer");
|
| 89 |
try {
|
|
|
|
| 1 |
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
+
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev"; // Your worker URL
|
| 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: Submits the structured report data as JSON
|
| 14 |
*/
|
| 15 |
+
async function handleReportUpload(event) {
|
| 16 |
+
event.preventDefault();
|
| 17 |
+
const uploadButton = document.getElementById("uploadButton");
|
| 18 |
+
const uploadStatus = document.getElementById("uploadStatus");
|
| 19 |
+
const report = {
|
| 20 |
+
title: document.getElementById('reportTitle').value,
|
| 21 |
+
description: document.getElementById('reportDescription').value,
|
| 22 |
+
body: document.getElementById('reportBody').value,
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
if (!report.title || !report.description || !report.body) {
|
| 26 |
+
uploadStatus.textContent = 'Please fill out all fields.';
|
| 27 |
+
return;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
uploadButton.disabled = true;
|
| 31 |
+
uploadButton.innerText = "Processing...";
|
| 32 |
+
uploadStatus.textContent = "Submitting and processing report. This may take a moment...";
|
| 33 |
+
|
| 34 |
+
try {
|
| 35 |
+
const response = await fetch(`${workerUrl}/api/upload`, {
|
| 36 |
+
method: 'POST',
|
| 37 |
+
headers: { 'Content-Type': 'application/json' },
|
| 38 |
+
body: JSON.stringify(report),
|
| 39 |
+
});
|
| 40 |
+
const data = await response.json();
|
| 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 |
+
* NEW: Loads the list of reports directly from the database
|
| 56 |
+
*/
|
| 57 |
+
async function loadReportsList() {
|
| 58 |
const reportsList = document.getElementById("reportsList");
|
| 59 |
reportsList.innerHTML = "<p>Fetching reports...</p>";
|
| 60 |
try {
|
| 61 |
+
const response = await fetch(`${workerUrl}/api/documents`);
|
| 62 |
+
if (!response.ok) throw new Error('Failed to fetch reports list.');
|
| 63 |
+
const documents = await response.json();
|
| 64 |
|
| 65 |
reportsList.innerHTML = "";
|
| 66 |
+
if (documents.length === 0) { reportsList.innerHTML = "<p>No reports have been added yet.</p>"; return; }
|
| 67 |
|
| 68 |
+
documents.forEach(doc => {
|
| 69 |
const entryDiv = document.createElement("div");
|
| 70 |
entryDiv.className = "report-entry";
|
| 71 |
+
entryDiv.innerHTML = `<h3>${doc.title}</h3><p>${doc.description}</p>`;
|
| 72 |
reportsList.appendChild(entryDiv);
|
| 73 |
});
|
| 74 |
} catch (error) {
|
| 75 |
reportsList.innerHTML = `<p>Could not load reports: ${error.message}</p>`;
|
| 76 |
}
|
| 77 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
// AI Query and History functions remain the same as the last working version
|
| 80 |
+
async function handleAiQuerySubmit() { /* ... function code is unchanged ... */ }
|
| 81 |
+
async function loadResearchHistory() { /* ... function code is unchanged ... */ }
|
| 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 {
|
|
|
|
| 103 |
submitButton.innerText = "Submit";
|
| 104 |
}
|
| 105 |
}
|
|
|
|
|
|
|
| 106 |
async function loadResearchHistory() {
|
| 107 |
const historyContainer = document.getElementById("historyContainer");
|
| 108 |
try {
|