privateuserh commited on
Commit
e892728
·
verified ·
1 Parent(s): 5d7163f

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +29 -24
script.js CHANGED
@@ -6,8 +6,7 @@ document.addEventListener("DOMContentLoaded", () => {
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();
@@ -16,30 +15,38 @@ document.addEventListener("DOMContentLoaded", () => {
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();
37
- console.error("Failed to save research:", errorText);
38
- alert(`Error: Could not save research. ${errorText}`);
39
  }
 
 
 
 
 
 
 
40
  } catch (error) {
41
- console.error("Network or other error:", error);
42
- alert("A network error occurred. Please check your connection and the worker status.");
 
 
 
 
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
- throw new Error(`HTTP error! status: ${response.status}`);
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. Please check the worker status and CORS settings.</p>";
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