Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,63 +1,80 @@
|
|
| 1 |
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
-
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev"; // Replace
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
const submitButton = document.getElementById("submitButton");
|
| 8 |
-
const researchQueryInput = document.getElementById("researchQuery");
|
| 9 |
-
const resultDisplay = document.getElementById("resultDisplay");
|
| 10 |
-
const historyContainer = document.getElementById("historyContainer");
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
async function handleFileUpload(event) {
|
| 13 |
event.preventDefault();
|
|
|
|
|
|
|
| 14 |
const file = fileInput.files[0];
|
| 15 |
if (!file) { uploadStatus.textContent = 'Please select a file.'; return; }
|
|
|
|
| 16 |
uploadStatus.textContent = `Uploading ${file.name}...`;
|
| 17 |
const formData = new FormData();
|
| 18 |
formData.append('file', file);
|
|
|
|
| 19 |
try {
|
| 20 |
const response = await fetch(`${workerUrl}/api/upload`, { method: 'POST', body: formData });
|
| 21 |
const data = await response.json();
|
| 22 |
if (!response.ok) throw new Error(data.error || 'Upload failed');
|
| 23 |
-
uploadStatus.textContent = `
|
| 24 |
-
|
| 25 |
-
await loadReportsList();
|
| 26 |
} catch (error) {
|
| 27 |
uploadStatus.textContent = `Error: ${error.message}`;
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
-
|
| 32 |
-
try {
|
| 33 |
-
const response = await fetch(`${workerUrl}/api/documents`);
|
| 34 |
-
if (!response.ok) throw new Error('Failed to fetch reports list.');
|
| 35 |
-
const documents = await response.json();
|
| 36 |
-
reportsList.innerHTML = "";
|
| 37 |
-
if (documents.length === 0) { reportsList.innerHTML = "<p>No reports uploaded yet.</p>"; return; }
|
| 38 |
-
documents.forEach(doc => {
|
| 39 |
-
const entryDiv = document.createElement("div");
|
| 40 |
-
entryDiv.className = "report-entry";
|
| 41 |
-
entryDiv.innerHTML = `<span>${doc.filename}</span><span class="status ${doc.status.toLowerCase()}">${doc.status}</span>`;
|
| 42 |
-
reportsList.appendChild(entryDiv);
|
| 43 |
-
});
|
| 44 |
-
} catch (error) {
|
| 45 |
-
reportsList.innerHTML = "<p>Could not load reports.</p>";
|
| 46 |
-
}
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
async function handleAiQuerySubmit() {
|
|
|
|
|
|
|
|
|
|
| 50 |
const query = researchQueryInput.value.trim();
|
| 51 |
-
if (!query) { alert("Please enter a
|
|
|
|
| 52 |
submitButton.disabled = true;
|
| 53 |
submitButton.innerText = "Thinking...";
|
| 54 |
resultDisplay.innerHTML = "<p>Generating response...</p>";
|
|
|
|
| 55 |
try {
|
| 56 |
const response = await fetch(`${workerUrl}/api/query`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ query: query }) });
|
| 57 |
const data = await response.json();
|
| 58 |
if (!response.ok) throw new Error(data.error || "An unknown error occurred.");
|
| 59 |
resultDisplay.innerText = data.result;
|
| 60 |
-
await loadResearchHistory();
|
| 61 |
} catch (error) {
|
| 62 |
resultDisplay.innerText = `Error: ${error.message}`;
|
| 63 |
} finally {
|
|
@@ -66,7 +83,9 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 66 |
}
|
| 67 |
}
|
| 68 |
|
|
|
|
| 69 |
async function loadResearchHistory() {
|
|
|
|
| 70 |
try {
|
| 71 |
const response = await fetch(`${workerUrl}/api/research`);
|
| 72 |
if (!response.ok) throw new Error('Failed to load history.');
|
|
@@ -83,9 +102,4 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 83 |
historyContainer.innerHTML = "<p>Error loading research history.</p>";
|
| 84 |
}
|
| 85 |
}
|
| 86 |
-
|
| 87 |
-
uploadForm.addEventListener("submit", handleFileUpload);
|
| 88 |
-
submitButton.addEventListener("click", handleAiQuerySubmit);
|
| 89 |
-
loadReportsList();
|
| 90 |
-
loadResearchHistory();
|
| 91 |
});
|
|
|
|
| 1 |
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
+
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev"; // Replace if your URL is different
|
| 3 |
+
|
| 4 |
+
// Event Listeners
|
| 5 |
+
document.getElementById("uploadForm").addEventListener("submit", handleFileUpload);
|
| 6 |
+
document.getElementById("submitButton").addEventListener("click", handleAiQuerySubmit);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
// Initial data load
|
| 9 |
+
loadReportsFromRss();
|
| 10 |
+
loadResearchHistory();
|
| 11 |
+
|
| 12 |
+
/**
|
| 13 |
+
* Fetches and displays the list of reports from the backend's RSS parser
|
| 14 |
+
*/
|
| 15 |
+
async function loadReportsFromRss() {
|
| 16 |
+
const reportsList = document.getElementById("reportsList");
|
| 17 |
+
reportsList.innerHTML = "<p>Fetching reports...</p>";
|
| 18 |
+
try {
|
| 19 |
+
const response = await fetch(`${workerUrl}/api/rss`);
|
| 20 |
+
if (!response.ok) throw new Error('Failed to fetch RSS feed.');
|
| 21 |
+
const items = await response.json();
|
| 22 |
+
|
| 23 |
+
reportsList.innerHTML = "";
|
| 24 |
+
if (items.length === 0) { reportsList.innerHTML = "<p>No reports found in the feed.</p>"; return; }
|
| 25 |
+
|
| 26 |
+
items.forEach(item => {
|
| 27 |
+
const entryDiv = document.createElement("div");
|
| 28 |
+
entryDiv.className = "report-entry";
|
| 29 |
+
entryDiv.innerHTML = `<a href="${item.link}" target="_blank" rel="noopener noreferrer">${item.title}</a><p>${item.description}</p>`;
|
| 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 |
+
// Simplified AI Query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(); // Refresh history after a successful query
|
| 78 |
} catch (error) {
|
| 79 |
resultDisplay.innerText = `Error: ${error.message}`;
|
| 80 |
} finally {
|
|
|
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
+
// Unchanged History Loader
|
| 87 |
async function loadResearchHistory() {
|
| 88 |
+
const historyContainer = document.getElementById("historyContainer");
|
| 89 |
try {
|
| 90 |
const response = await fetch(`${workerUrl}/api/research`);
|
| 91 |
if (!response.ok) throw new Error('Failed to load history.');
|
|
|
|
| 102 |
historyContainer.innerHTML = "<p>Error loading research history.</p>";
|
| 103 |
}
|
| 104 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
});
|