-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
105 lines (83 loc) · 3.18 KB
/
script.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Menu
var navLinks = document.getElementById("navLinks")
function showMenu() {
navLinks.style.right = "0"
}
function hideMenu() {
navLinks.style.right = "-200px"
}
// Chat
const chatInput = document.querySelector(".chat-input textarea")
const sendChatBtn = document.querySelector(".chat-input span")
const chatbox = document.querySelector(".chatbox")
let userMessage = null;
let assist_id = "asst_Iydh5lFCJn3e2U37sC5iPzDS";
let thread_id = null;
let intent_thread_id = null;
let instructions_bool=true;
let responseMessage;
const createChatLi = (message, className) => {
const chatLi = document.createElement('li')
chatLi.classList.add("chat",className);
let chatContent = className === "outgoing" ? `<p>${message}</p>` : `<span class="material-symbols-outlined">smart_toy</span><p>${message}</p>`
chatLi.innerHTML = chatContent;
return chatLi;
}
const generateResponse = (incomingChatLi) => {
const API_URL = "http://localhost:8000/get_response";
const messageElement = incomingChatLi.querySelector("p");
const requestBody = {
message: userMessage,
assist_id: assist_id,
thread_id:thread_id,
intent_thread_id:intent_thread_id,
instructions_bool: instructions_bool
};
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody)
};
responseMessage = fetch(API_URL, requestOptions)
.then(res => {
if (!res.ok) {
throw new Error('Network response was not ok');
}
return res.json()
}).then(data => {
console.log(data)
if (typeof data.message == 'string') {
messageElement.textContent = data.message;
console.log(messageElement.textContent)
}
else if (Array.isArray(data.message)) {
messageElement.textContent = data.message.join('\n');
console.log(messageElement.textContent)
}
else {
messageElement.textContent = "Oops! Something went wrong. I can not assist you at the moment, consider contacting the human support team! :-)"
console.log("'else' happend inside fetch of responseMessage.")
console.log(data.message)
}
thread_id = data.thread_id
intent_thread_id = data.intent_thread_id
instructions_bool = data.instructions_bool
console.log(messageElement.textContent)
}).catch((error) => {
console.error('Error in catch:', error)
messageElement.textContent = "Oops! Something went wrong. I can not assist you at the moment, consider contacting the human support team! :-)";
});
}
const handleChat = () => {
userMessage = chatInput.value.trim();
if(!userMessage) return;
chatbox.appendChild(createChatLi(userMessage, 'outgoing'));
setTimeout(async () => {
const incomingChatLi = createChatLi("Thinking...", "incoming")
chatbox.appendChild(incomingChatLi);
await generateResponse(incomingChatLi);
}, 600);
}
sendChatBtn.addEventListener("click", handleChat)