Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
@@ -1,27 +1,107 @@
|
|
1 |
document.addEventListener("DOMContentLoaded", () => {
|
2 |
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev";
|
|
|
|
|
3 |
const submitButton = document.getElementById("submitButton");
|
4 |
const researchQueryInput = document.getElementById("researchQuery");
|
5 |
const resultDisplay = document.getElementById("resultDisplay");
|
6 |
const historyContainer = document.getElementById("historyContainer");
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
/**
|
9 |
-
*
|
10 |
*/
|
11 |
-
async function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
const query = researchQueryInput.value.trim();
|
13 |
if (!query) {
|
14 |
alert("Please enter a research query.");
|
15 |
return;
|
16 |
}
|
17 |
|
18 |
-
// Show a loading state
|
19 |
submitButton.disabled = true;
|
20 |
submitButton.innerText = "Thinking...";
|
21 |
resultDisplay.innerHTML = "<p>Generating response...</p>";
|
22 |
|
23 |
try {
|
24 |
-
// Call the new AI endpoint
|
25 |
const response = await fetch(`${workerUrl}/api/query`, {
|
26 |
method: "POST",
|
27 |
headers: { "Content-Type": "application/json" },
|
@@ -29,44 +109,28 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
29 |
});
|
30 |
|
31 |
const data = await response.json();
|
32 |
-
|
33 |
-
if (!response.ok) {
|
34 |
-
throw new Error(data.error || "An unknown error occurred.");
|
35 |
-
}
|
36 |
-
|
37 |
-
// Display the real result from the AI
|
38 |
resultDisplay.innerText = data.result;
|
39 |
-
|
40 |
-
// Reload history to include the new entry
|
41 |
await loadResearchHistory();
|
42 |
-
|
43 |
} catch (error) {
|
44 |
console.error("Error fetching AI response:", error);
|
45 |
resultDisplay.innerText = `Error: ${error.message}`;
|
46 |
} finally {
|
47 |
-
// Restore the button
|
48 |
submitButton.disabled = false;
|
49 |
submitButton.innerText = "Submit";
|
50 |
}
|
51 |
}
|
52 |
|
53 |
-
/**
|
54 |
-
* Loads and displays the research history from the backend.
|
55 |
-
*/
|
56 |
async function loadResearchHistory() {
|
57 |
try {
|
58 |
-
// Use the correct endpoint for fetching history
|
59 |
const response = await fetch(`${workerUrl}/api/research`);
|
60 |
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
61 |
-
|
62 |
const logs = await response.json();
|
63 |
-
historyContainer.innerHTML = "";
|
64 |
-
|
65 |
if (logs.length === 0) {
|
66 |
historyContainer.innerHTML = "<p>No research history found.</p>";
|
67 |
return;
|
68 |
}
|
69 |
-
|
70 |
logs.forEach(log => {
|
71 |
const entryDiv = document.createElement("div");
|
72 |
entryDiv.className = "history-entry";
|
@@ -84,8 +148,10 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
84 |
}
|
85 |
|
86 |
// Event Listeners
|
87 |
-
|
|
|
88 |
|
89 |
-
// Initial load
|
|
|
90 |
loadResearchHistory();
|
91 |
});
|
|
|
1 |
document.addEventListener("DOMContentLoaded", () => {
|
2 |
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev";
|
3 |
+
|
4 |
+
// Chat elements
|
5 |
const submitButton = document.getElementById("submitButton");
|
6 |
const researchQueryInput = document.getElementById("researchQuery");
|
7 |
const resultDisplay = document.getElementById("resultDisplay");
|
8 |
const historyContainer = document.getElementById("historyContainer");
|
9 |
|
10 |
+
// Document management elements
|
11 |
+
const uploadForm = document.getElementById("uploadForm");
|
12 |
+
const fileInput = document.getElementById("fileInput");
|
13 |
+
const uploadStatus = document.getElementById("uploadStatus");
|
14 |
+
const reportsList = document.getElementById("reportsList");
|
15 |
+
|
16 |
+
/**
|
17 |
+
* Handles file upload
|
18 |
+
*/
|
19 |
+
async function handleFileUpload(event) {
|
20 |
+
event.preventDefault();
|
21 |
+
const file = fileInput.files[0];
|
22 |
+
if (!file) {
|
23 |
+
uploadStatus.textContent = 'Please select a file to upload.';
|
24 |
+
uploadStatus.style.color = 'red';
|
25 |
+
return;
|
26 |
+
}
|
27 |
+
|
28 |
+
uploadStatus.textContent = `Uploading ${file.name}...`;
|
29 |
+
uploadStatus.style.color = 'blue';
|
30 |
+
|
31 |
+
try {
|
32 |
+
const formData = new FormData();
|
33 |
+
formData.append('file', file);
|
34 |
+
|
35 |
+
// Note: We're sending FormData, not JSON
|
36 |
+
const response = await fetch(`${workerUrl}/api/upload`, {
|
37 |
+
method: 'POST',
|
38 |
+
body: formData,
|
39 |
+
});
|
40 |
+
|
41 |
+
const data = await response.json();
|
42 |
+
|
43 |
+
if (!response.ok) {
|
44 |
+
throw new Error(data.error || 'Upload failed');
|
45 |
+
}
|
46 |
+
|
47 |
+
uploadStatus.textContent = `Successfully uploaded ${file.name}!`;
|
48 |
+
uploadStatus.style.color = 'green';
|
49 |
+
uploadForm.reset();
|
50 |
+
await loadReportsList(); // Refresh the list of reports
|
51 |
+
|
52 |
+
} catch (error) {
|
53 |
+
console.error("Upload error:", error);
|
54 |
+
uploadStatus.textContent = `Error: ${error.message}`;
|
55 |
+
uploadStatus.style.color = 'red';
|
56 |
+
}
|
57 |
+
}
|
58 |
+
|
59 |
/**
|
60 |
+
* Loads the list of available research reports
|
61 |
*/
|
62 |
+
async function loadReportsList() {
|
63 |
+
try {
|
64 |
+
const response = await fetch(`${workerUrl}/api/documents`);
|
65 |
+
if (!response.ok) throw new Error('Failed to fetch reports list.');
|
66 |
+
|
67 |
+
const documents = await response.json();
|
68 |
+
reportsList.innerHTML = ""; // Clear existing list
|
69 |
+
|
70 |
+
if (documents.length === 0) {
|
71 |
+
reportsList.innerHTML = "<p>No reports uploaded yet.</p>";
|
72 |
+
return;
|
73 |
+
}
|
74 |
+
|
75 |
+
documents.forEach(doc => {
|
76 |
+
const entryDiv = document.createElement("div");
|
77 |
+
entryDiv.className = "report-entry";
|
78 |
+
entryDiv.innerHTML = `
|
79 |
+
<span>${doc.filename}</span>
|
80 |
+
<span class="status ${doc.status.toLowerCase()}">${doc.status}</span>
|
81 |
+
`;
|
82 |
+
reportsList.appendChild(entryDiv);
|
83 |
+
});
|
84 |
+
|
85 |
+
} catch (error) {
|
86 |
+
console.error(error);
|
87 |
+
reportsList.innerHTML = "<p>Could not load reports.</p>";
|
88 |
+
}
|
89 |
+
}
|
90 |
+
|
91 |
+
// --- AI Chat Functions (from previous step, unchanged) ---
|
92 |
+
|
93 |
+
async function handleAiQuerySubmit() {
|
94 |
const query = researchQueryInput.value.trim();
|
95 |
if (!query) {
|
96 |
alert("Please enter a research query.");
|
97 |
return;
|
98 |
}
|
99 |
|
|
|
100 |
submitButton.disabled = true;
|
101 |
submitButton.innerText = "Thinking...";
|
102 |
resultDisplay.innerHTML = "<p>Generating response...</p>";
|
103 |
|
104 |
try {
|
|
|
105 |
const response = await fetch(`${workerUrl}/api/query`, {
|
106 |
method: "POST",
|
107 |
headers: { "Content-Type": "application/json" },
|
|
|
109 |
});
|
110 |
|
111 |
const data = await response.json();
|
112 |
+
if (!response.ok) throw new Error(data.error || "An unknown error occurred.");
|
|
|
|
|
|
|
|
|
|
|
113 |
resultDisplay.innerText = data.result;
|
|
|
|
|
114 |
await loadResearchHistory();
|
|
|
115 |
} catch (error) {
|
116 |
console.error("Error fetching AI response:", error);
|
117 |
resultDisplay.innerText = `Error: ${error.message}`;
|
118 |
} finally {
|
|
|
119 |
submitButton.disabled = false;
|
120 |
submitButton.innerText = "Submit";
|
121 |
}
|
122 |
}
|
123 |
|
|
|
|
|
|
|
124 |
async function loadResearchHistory() {
|
125 |
try {
|
|
|
126 |
const response = await fetch(`${workerUrl}/api/research`);
|
127 |
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
128 |
const logs = await response.json();
|
129 |
+
historyContainer.innerHTML = "";
|
|
|
130 |
if (logs.length === 0) {
|
131 |
historyContainer.innerHTML = "<p>No research history found.</p>";
|
132 |
return;
|
133 |
}
|
|
|
134 |
logs.forEach(log => {
|
135 |
const entryDiv = document.createElement("div");
|
136 |
entryDiv.className = "history-entry";
|
|
|
148 |
}
|
149 |
|
150 |
// Event Listeners
|
151 |
+
uploadForm.addEventListener("submit", handleFileUpload);
|
152 |
+
submitButton.addEventListener("click", handleAiQuerySubmit);
|
153 |
|
154 |
+
// Initial load
|
155 |
+
loadReportsList();
|
156 |
loadResearchHistory();
|
157 |
});
|