-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (73 loc) · 3.08 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
// Add your JavaScript code here
const messageContainer = document.querySelector('#message-container');
const messageInput = document.querySelector('#message-input');
messageInput.addEventListener('keyup', function(event) {
if (event.key === 'Enter') {
sendMessage();
}
});
const sendBtn = document.querySelector('#send-btn');
sendBtn.addEventListener('click', sendMessage);
const resetBtn = document.querySelector('#reset-btn');
resetBtn.addEventListener('click', resetConversation);
function renderSpecialThings( message ) {
// Trim away whitespace at the start of the message
message = message.replace(/^\s+/, '');
// Convert code sample to HTML entities
message = message.replace(/[\u00A0-\u9999<>&]/g, function(i) {
return '&#'+i.charCodeAt(0)+';';
});
// Replace line breaks in message with html breaks
message = message.replace(/\n/g, '<br>');
// Restore linebreaks in code samples - also catching the case where --CODESTART-- is streamed already but the end is not yet
message = message.replace(/--CODESTART--((?:(?!--CODESTART--)[\s\S])*?)(?:--CODEEND--|$)/g, function(match, group) {
return match.replace(/<br>/g, "\n");
});
// Replace closing and opening statements in PHP examples
message = message.replace(/--CODESTART--\s+/g, '<pre><code>');
message = message.replace(/--CODEEND--/g, '</code></pre>');
return message;
}
function uniqueId() {
const dateString = Date.now().toString(36);
const randomness = Math.random().toString(36).substr(2);
return dateString + randomness;
}
let current_session_id = uniqueId();
function sendMessage() {
const message = messageInput.value.trim();
if (!message) {
return;
}
// Create question element
const questionContainer = window.document.createElement('div');
questionContainer.classList.add('question');
questionContainer.innerText = message;
messageContainer.append(questionContainer);
// Create answer element
const answerContainer = window.document.createElement('div');
answerContainer.classList.add('answer');
messageContainer.append(answerContainer);
messageInput.value = '';
messageContainer.scrollTop = messageContainer.scrollHeight;
const xhr = new XMLHttpRequest();
xhr.open('POST', 'server.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if ((xhr.readyState === 3 || xhr.readyState === 4) && xhr.status === 200) {
const response = xhr.responseText;
answerContainer.innerHTML = renderSpecialThings( response );
messageContainer.scrollTop = messageContainer.scrollHeight;
}
hljs.highlightAll();
};
xhr.send('sess=' + current_session_id + '&message=' + encodeURIComponent(message));
}
function resetConversation() {
const confirmed = confirm('Are you sure you want to start a new conversation? All history will be lost!');
if (!confirmed) {
return;
}
current_session_id = uniqueId();
messageContainer.innerText = '';
}