Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
document.addEventListener("DOMContentLoaded", () => {
|
2 |
-
// Configuration
|
3 |
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev";
|
4 |
const submitButton = document.getElementById("submitButton");
|
5 |
const researchQueryInput = document.getElementById("researchQuery");
|
@@ -7,28 +6,31 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
7 |
const historyContainer = document.getElementById("historyContainer");
|
8 |
|
9 |
/**
|
10 |
-
*
|
|
|
11 |
*/
|
12 |
async function handleSubmit() {
|
13 |
const query = researchQueryInput.value.trim();
|
14 |
-
|
15 |
-
|
16 |
-
if (!query || !result) {
|
17 |
-
alert("Please enter a query and ensure there is a result to save.");
|
18 |
return;
|
19 |
}
|
20 |
|
|
|
|
|
|
|
|
|
|
|
21 |
try {
|
22 |
const response = await fetch(`${workerUrl}/api/research`, {
|
23 |
method: "POST",
|
24 |
headers: { "Content-Type": "application/json" },
|
25 |
-
body: JSON.stringify({ query, result }),
|
26 |
});
|
27 |
|
28 |
if (response.ok) {
|
29 |
-
// Clear
|
30 |
researchQueryInput.value = "";
|
31 |
-
resultDisplay.innerHTML = "";
|
32 |
await loadResearchHistory();
|
33 |
} else {
|
34 |
const errorText = await response.text();
|
@@ -45,6 +47,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
45 |
* Loads and displays the research history from the backend.
|
46 |
*/
|
47 |
async function loadResearchHistory() {
|
|
|
48 |
try {
|
49 |
const response = await fetch(`${workerUrl}/api/research`);
|
50 |
if (!response.ok) {
|
@@ -71,7 +74,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
71 |
});
|
72 |
} catch (error) {
|
73 |
console.error("Failed to load research history:", error);
|
74 |
-
historyContainer.innerHTML = "<p>Error loading research history.</p>";
|
75 |
}
|
76 |
}
|
77 |
|
|
|
1 |
document.addEventListener("DOMContentLoaded", () => {
|
|
|
2 |
const workerUrl = "https://ctmresearchagent.aiagents.workers.dev";
|
3 |
const submitButton = document.getElementById("submitButton");
|
4 |
const researchQueryInput = document.getElementById("researchQuery");
|
|
|
6 |
const historyContainer = document.getElementById("historyContainer");
|
7 |
|
8 |
/**
|
9 |
+
* --- FIX: Updated submission logic ---
|
10 |
+
* This function now simulates a result and saves it.
|
11 |
*/
|
12 |
async function handleSubmit() {
|
13 |
const query = researchQueryInput.value.trim();
|
14 |
+
if (!query) {
|
15 |
+
alert("Please enter a research query.");
|
|
|
|
|
16 |
return;
|
17 |
}
|
18 |
|
19 |
+
// 1. Simulate a result. (The real "AI Agent" logic would go here)
|
20 |
+
const simulatedResult = `Research results for "${query}" would be generated by an AI and displayed here. This is a placeholder.`;
|
21 |
+
resultDisplay.innerText = simulatedResult;
|
22 |
+
|
23 |
+
// 2. Save the query and the new result to the database.
|
24 |
try {
|
25 |
const response = await fetch(`${workerUrl}/api/research`, {
|
26 |
method: "POST",
|
27 |
headers: { "Content-Type": "application/json" },
|
28 |
+
body: JSON.stringify({ query: query, result: simulatedResult }),
|
29 |
});
|
30 |
|
31 |
if (response.ok) {
|
32 |
+
// 3. Clear input and reload the history to show the new entry.
|
33 |
researchQueryInput.value = "";
|
|
|
34 |
await loadResearchHistory();
|
35 |
} else {
|
36 |
const errorText = await response.text();
|
|
|
47 |
* Loads and displays the research history from the backend.
|
48 |
*/
|
49 |
async function loadResearchHistory() {
|
50 |
+
historyContainer.innerHTML = "<p>Loading history...</p>"; // Provide user feedback
|
51 |
try {
|
52 |
const response = await fetch(`${workerUrl}/api/research`);
|
53 |
if (!response.ok) {
|
|
|
74 |
});
|
75 |
} catch (error) {
|
76 |
console.error("Failed to load research history:", error);
|
77 |
+
historyContainer.innerHTML = "<p>Error loading research history. Please check the worker status and CORS settings.</p>";
|
78 |
}
|
79 |
}
|
80 |
|