-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (39 loc) · 1.23 KB
/
Copy pathscript.js
File metadata and controls
49 lines (39 loc) · 1.23 KB
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
// NutriMatch AI Chat Client Script
const chatBox = document.getElementById("chat");
// Add message to chat UI
function addMessage(text, sender = "ai") {
const bubble = document.createElement("div");
bubble.className =
`message p-3 rounded-xl max-w-[80%] ${
sender === "user"
? "ml-auto bg-orange-500 text-white"
: "bg-white shadow border"
}`;
bubble.textContent = text;
chatBox.appendChild(bubble);
chatBox.scrollTop = chatBox.scrollHeight;
}
// Send chat request
async function sendMessage() {
const input = document.getElementById("userInput");
const text = input.value.trim();
if (!text) return;
addMessage(text, "user");
input.value = "";
addMessage("Thinking… 🤔");
try {
const res = await fetch("http://localhost:3000/chat", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ query: text })
});
const data = await res.json();
chatBox.lastChild.remove(); // remove thinking...
addMessage(data.reply, "ai");
} catch (error) {
chatBox.lastChild.remove();
addMessage("⚠️ AI Server not running — please start server.js", "ai");
}
}