Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
@@ -1,119 +1,64 @@
|
|
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 |
-
|
30 |
-
|
31 |
try {
|
32 |
-
const
|
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();
|
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 = "";
|
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" },
|
108 |
-
body: JSON.stringify({ query: query }),
|
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;
|
@@ -124,34 +69,23 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
124 |
async function loadResearchHistory() {
|
125 |
try {
|
126 |
const response = await fetch(`${workerUrl}/api/research`);
|
127 |
-
if (!response.ok) throw new Error(
|
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";
|
137 |
-
entryDiv.innerHTML =
|
138 |
-
<p><strong>Query:</strong> ${log.query}</p>
|
139 |
-
<p><strong>Result:</strong> ${log.result}</p>
|
140 |
-
<p><small><em>${new Date(log.timestamp).toLocaleString()}</em></small></p>
|
141 |
-
`;
|
142 |
historyContainer.appendChild(entryDiv);
|
143 |
});
|
144 |
} catch (error) {
|
145 |
-
console.error("Failed to load research history:", error);
|
146 |
historyContainer.innerHTML = "<p>Error loading research history.</p>";
|
147 |
}
|
148 |
}
|
149 |
|
150 |
-
// Event Listeners
|
151 |
uploadForm.addEventListener("submit", handleFileUpload);
|
152 |
submitButton.addEventListener("click", handleAiQuerySubmit);
|
153 |
-
|
154 |
-
// Initial load
|
155 |
loadReportsList();
|
156 |
loadResearchHistory();
|
157 |
});
|
|
|
1 |
document.addEventListener("DOMContentLoaded", () => {
|
2 |
+
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev"; // Replace with your worker URL if different
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
const uploadForm = document.getElementById("uploadForm");
|
4 |
const fileInput = document.getElementById("fileInput");
|
5 |
const uploadStatus = document.getElementById("uploadStatus");
|
6 |
const reportsList = document.getElementById("reportsList");
|
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 = `Successfully uploaded ${file.name}!`;
|
|
|
24 |
uploadForm.reset();
|
25 |
+
await loadReportsList();
|
|
|
26 |
} catch (error) {
|
|
|
27 |
uploadStatus.textContent = `Error: ${error.message}`;
|
|
|
28 |
}
|
29 |
}
|
30 |
|
|
|
|
|
|
|
31 |
async function loadReportsList() {
|
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 research query."); return; }
|
|
|
|
|
|
|
|
|
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 {
|
64 |
submitButton.disabled = false;
|
|
|
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.');
|
73 |
const logs = await response.json();
|
74 |
historyContainer.innerHTML = "";
|
75 |
+
if (logs.length === 0) { historyContainer.innerHTML = "<p>No research history found.</p>"; return; }
|
|
|
|
|
|
|
76 |
logs.forEach(log => {
|
77 |
const entryDiv = document.createElement("div");
|
78 |
entryDiv.className = "history-entry";
|
79 |
+
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>`;
|
|
|
|
|
|
|
|
|
80 |
historyContainer.appendChild(entryDiv);
|
81 |
});
|
82 |
} catch (error) {
|
|
|
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 |
});
|