privateuserh commited on
Commit
ede5511
·
verified ·
1 Parent(s): f3680b1

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +83 -0
script.js CHANGED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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");
6
+ const resultDisplay = document.getElementById("resultDisplay");
7
+ const historyContainer = document.getElementById("historyContainer");
8
+
9
+ /**
10
+ * Handles the form submission to save data.
11
+ */
12
+ async function handleSubmit() {
13
+ const query = researchQueryInput.value.trim();
14
+ const result = resultDisplay.innerText.trim(); // Get text from the contenteditable div
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 inputs and reload history
30
+ researchQueryInput.value = "";
31
+ resultDisplay.innerHTML = "";
32
+ await loadResearchHistory();
33
+ } else {
34
+ const errorText = await response.text();
35
+ console.error("Failed to save research:", errorText);
36
+ alert(`Error: Could not save research. ${errorText}`);
37
+ }
38
+ } catch (error) {
39
+ console.error("Network or other error:", error);
40
+ alert("A network error occurred. Please check your connection and the worker status.");
41
+ }
42
+ }
43
+
44
+ /**
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) {
51
+ throw new Error(`HTTP error! status: ${response.status}`);
52
+ }
53
+ const logs = await response.json();
54
+
55
+ historyContainer.innerHTML = ""; // Clear existing history
56
+
57
+ if (logs.length === 0) {
58
+ historyContainer.innerHTML = "<p>No research history found.</p>";
59
+ return;
60
+ }
61
+
62
+ logs.forEach(log => {
63
+ const entryDiv = document.createElement("div");
64
+ entryDiv.className = "history-entry";
65
+ entryDiv.innerHTML = `
66
+ <p><strong>Query:</strong> ${log.query}</p>
67
+ <p><strong>Result:</strong> ${log.result}</p>
68
+ <p><small><em>${new Date(log.timestamp).toLocaleString()}</em></small></p>
69
+ `;
70
+ historyContainer.appendChild(entryDiv);
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
+
78
+ // Event Listeners
79
+ submitButton.addEventListener("click", handleSubmit);
80
+
81
+ // Initial load of history
82
+ loadResearchHistory();
83
+ });