File size: 1,908 Bytes
199185f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289c7c1
199185f
289c7c1
 
199185f
 
289c7c1
 
 
 
 
 
199185f
 
289c7c1
199185f
 
289c7c1
 
 
 
 
 
 
 
199185f
289c7c1
199185f
289c7c1
199185f
289c7c1
 
199185f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const form = document.getElementById("qa-form");
const input = document.getElementById("question-input");
const currentAnswer = document.getElementById("current-answer");
const historyList = document.getElementById("history-list");

form.addEventListener("submit", async (e) => {
  e.preventDefault();

  const question = input.value.trim();
  if (!question) return;

  // Show loading
  currentAnswer.style.display = "block";
  currentAnswer.textContent = "Consulting the Hogwarts archives...";

  const formData = new FormData();
  formData.append("query", question);

  try {
    const res = await fetch("/ask", {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body: new URLSearchParams({ query: question })
    });

    const text = await res.text();
    console.log("RAW RESPONSE:", text);

    if (!res.ok) {
      currentAnswer.textContent = `Error ${res.status}: ${text}`;
      return;
    }

    const data = JSON.parse(text);
    const answer = data.answer;

    // Move to history
    const item = document.createElement("div");
    item.className = "history-item";
    item.innerHTML = `
      <div class="history-question">${question}</div>
      <div class="history-answer">${answer}</div>
    `;
    historyList.prepend(item);

    currentAnswer.style.display = "none";
    input.value = "";

  } catch (err) {
    console.error(err);
    currentAnswer.textContent = "JS error: " + err.message;
  }
});

function addToHistory(question, answer) {
  const item = document.createElement("div");
  item.className = "history-item";

  item.innerHTML = `
    <div class="history-question">Q: ${escapeHTML(question)}</div>
    <div class="history-answer">${escapeHTML(answer)}</div>
  `;

  historyList.prepend(item);
}

function escapeHTML(text) {
  const div = document.createElement("div");
  div.textContent = text;
  return div.innerHTML;
}