-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
97 lines (76 loc) · 2.5 KB
/
app.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
const sendButton = document.getElementById("sendButton");
const chatInput = document.getElementById("chatInput");
const cardsTable = document.getElementById("cardsTable");
const createCard = (title, content) => {
return `<div class="card">
<h3>${title}</h3>
<p>${content}</p>
</div>`;
};
const parseResponse = (response) => {
const lines = response.split("\n");
let sections = [];
let currentSection = { title: "", content: "" };
let sessionNameFound = false;
lines.forEach((line) => {
if (line) {
if (!sessionNameFound) {
currentSection.title = line;
sessionNameFound = true;
} else if (line.endsWith(":")) {
if (currentSection.title) {
sections.push(currentSection);
}
currentSection = { title: line, content: "" };
} else {
currentSection.content += (currentSection.content ? "<br>" : "") + line;
}
}
});
if (currentSection.title) {
sections.push(currentSection);
}
return sections;
};
async function sendMessage(prompt) {
try {
const loading = document.getElementById("loading");
loading.style.display = "block"; // Show the loading animation
const response = await axios.post("/chat", { prompt });
const sections = parseResponse(response.data);
loading.style.display = "none"; // Hide the loading animation
displayProgram(sections);
} catch (error) {
console.error(`Error: ${error.message}`);
}
}
sendButton.addEventListener("click", async () => {
const prompt = chatInput.value;
if (!prompt) {
return;
}
// Display user input
const userMessage = document.createElement("div");
userMessage.className = "message user-message";
userMessage.innerHTML = prompt;
messages.appendChild(userMessage);
try {
const response = await axios.post("http://localhost:8000/chat", { prompt });
const generatedResponse = response.data.trim();
const sections = parseResponse(generatedResponse);
let cards = "";
sections.forEach((section) => {
cards += createCard(section.title, section.content);
});
// Display bot response
const botMessage = document.createElement("div");
botMessage.className = "message bot-message";
botMessage.innerHTML = `<div class="cards-container">${cards}</div>`;
messages.appendChild(botMessage);
// Scroll to the bottom of the messages container
messages.scrollTop = messages.scrollHeight;
} catch (error) {
console.error(`Error: ${error.message}`);
}
chatInput.value = "";
});