-
Notifications
You must be signed in to change notification settings - Fork 80
/
script.js
180 lines (154 loc) · 5.46 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
if (getCookie("id") == "") {
uuid = uuidv4()
document.cookie = "id=" + uuid
document.getElementById("id").value = uuid
} else {
document.getElementById("id").value = getCookie("id");
}
const idSession = get(".id_session");
const USER_ID = document.getElementById("id").value;
idSession.textContent = USER_ID
getHistory()
const msgerForm = get(".msger-inputarea");
const msgerInput = get(".msger-input");
const msgerChat = get(".msger-chat");
const msgerSendBtn = get(".msger-send-btn");
// Icons made by Freepik from www.flaticon.com
const BOT_IMG = "./chatgpt.svg";
const PERSON_IMG = "https://api.dicebear.com/5.x/micah/svg?seed=" + document.getElementById("id").value
const BOT_NAME = "ChatGPT";
const PERSON_NAME = "You";
// Function to delete chat history records for a user ID using the API
function deleteChatHistory(userId) {
if (!confirm("Are you sure? Your Session and History will delete for good.")) {
return false
}
fetch('/api.php?user=' + USER_ID, {
method: 'DELETE',
headers: {'Content-Type': 'application/json'}
})
.then(response => {
if (!response.ok) {
throw new Error('Error deleting chat history: ' + response.statusText);
}
deleteAllCookies()
location.reload(); // Reload the page to update the chat history table
})
.catch(error => console.error(error));
}
// Event listener for the Delete button click
const deleteButton = document.querySelector('#delete-button');
deleteButton.addEventListener('click', event => {
event.preventDefault();
deleteChatHistory(USER_ID);
});
msgerForm.addEventListener("submit", event => {
event.preventDefault();
const msgText = msgerInput.value;
if (!msgText) return;
appendMessage(PERSON_NAME, PERSON_IMG, "right", msgText);
msgerInput.value = "";
sendMsg(msgText)
});
function getHistory() {
var formData = new FormData();
formData.append('user_id', USER_ID);
fetch('/api.php', {method: 'POST', body: formData})
.then(response => response.json())
.then(chatHistory => {
for (const row of chatHistory) {
appendMessage(PERSON_NAME, PERSON_IMG, "right", row.human);
appendMessage(BOT_NAME, BOT_IMG, "left", row.ai, "");
}
})
.catch(error => console.error(error));
}
function appendMessage(name, img, side, text, id) {
// Simple solution for small apps
const msgHTML = `
<div class="msg ${side}-msg">
<div class="msg-img" style="background-image: url(${img})"></div>
<div class="msg-bubble">
<div class="msg-info">
<div class="msg-info-name">${name}</div>
<div class="msg-info-time">${formatDate(new Date())}</div>
</div>
<div class="msg-text" id=${id}>${text}</div>
</div>
</div>
`;
msgerChat.insertAdjacentHTML("beforeend", msgHTML);
msgerChat.scrollTop += 500;
}
function sendMsg(msg) {
msgerSendBtn.disabled = true
var formData = new FormData();
formData.append('msg', msg);
formData.append('user_id', USER_ID);
fetch('/send-message.php', {method: 'POST', body: formData})
.then(response => response.json())
.then(data => {
let uuid = uuidv4()
const eventSource = new EventSource(`/event-stream.php?chat_history_id=${data.id}&id=${encodeURIComponent(USER_ID)}`);
appendMessage(BOT_NAME, BOT_IMG, "left", "", uuid);
const div = document.getElementById(uuid);
eventSource.onmessage = function (e) {
if (e.data == "[DONE]") {
msgerSendBtn.disabled = false
eventSource.close();
} else {
let txt = JSON.parse(e.data).choices[0].delta.content
if (txt !== undefined) {
div.innerHTML += txt.replace(/(?:\r\n|\r|\n)/g, '<br>');
}
}
};
eventSource.onerror = function (e) {
msgerSendBtn.disabled = false
console.log(e);
eventSource.close();
};
})
.catch(error => console.error(error));
}
// Utils
function get(selector, root = document) {
return root.querySelector(selector);
}
function formatDate(date) {
const h = "0" + date.getHours();
const m = "0" + date.getMinutes();
return `${h.slice(-2)}:${m.slice(-2)}`;
}
function random(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function uuidv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
function deleteAllCookies() {
const cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i];
const eqPos = cookie.indexOf("=");
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}