Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -6,8 +6,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 6 |
const historyContainer = document.getElementById("historyContainer");
|
| 7 |
|
| 8 |
/**
|
| 9 |
-
* ---
|
| 10 |
-
* This function now simulates a result and saves it.
|
| 11 |
*/
|
| 12 |
async function handleSubmit() {
|
| 13 |
const query = researchQueryInput.value.trim();
|
|
@@ -16,30 +15,38 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 16 |
return;
|
| 17 |
}
|
| 18 |
|
| 19 |
-
//
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
// 2. Save the query and the new result to the database.
|
| 24 |
try {
|
| 25 |
-
|
|
|
|
| 26 |
method: "POST",
|
| 27 |
headers: { "Content-Type": "application/json" },
|
| 28 |
-
body: JSON.stringify({ query: query
|
| 29 |
});
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
} else {
|
| 36 |
-
const errorText = await response.text();
|
| 37 |
-
console.error("Failed to save research:", errorText);
|
| 38 |
-
alert(`Error: Could not save research. ${errorText}`);
|
| 39 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
} catch (error) {
|
| 41 |
-
console.error("
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
|
@@ -47,14 +54,12 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 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) {
|
| 54 |
-
|
| 55 |
-
}
|
| 56 |
const logs = await response.json();
|
| 57 |
-
|
| 58 |
historyContainer.innerHTML = ""; // Clear existing history
|
| 59 |
|
| 60 |
if (logs.length === 0) {
|
|
@@ -74,7 +79,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 74 |
});
|
| 75 |
} catch (error) {
|
| 76 |
console.error("Failed to load research history:", error);
|
| 77 |
-
historyContainer.innerHTML = "<p>Error loading research history
|
| 78 |
}
|
| 79 |
}
|
| 80 |
|
|
|
|
| 6 |
const historyContainer = document.getElementById("historyContainer");
|
| 7 |
|
| 8 |
/**
|
| 9 |
+
* --- UPDATED: Submits query to the AI backend ---
|
|
|
|
| 10 |
*/
|
| 11 |
async function handleSubmit() {
|
| 12 |
const query = researchQueryInput.value.trim();
|
|
|
|
| 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" },
|
| 28 |
+
body: JSON.stringify({ query: query }),
|
| 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 |
|
|
|
|
| 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 = ""; // Clear existing history
|
| 64 |
|
| 65 |
if (logs.length === 0) {
|
|
|
|
| 79 |
});
|
| 80 |
} catch (error) {
|
| 81 |
console.error("Failed to load research history:", error);
|
| 82 |
+
historyContainer.innerHTML = "<p>Error loading research history.</p>";
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|