-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
325 lines (288 loc) · 14.9 KB
/
index.html
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Tailwind CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<title>Barend Botje</title>
</head>
<body class="bg-blue-50 p-4 h-screen flex items-stretch">
<div class="min-w-96 max-w-4xl mx-auto border border-2 border-blue-800 rounded-xl overflow-hidden flex flex-col justify-between">
<header class="h-20 w-full bg-blue-200 flex flex-row justify-start space-x-4 px-1">
<div class="py-1 flex flex-shrink-0 w-20">
<img src="barend.jpg" alt="Barend Mons" class="block w-18 w-auto rounded-full border border-blue-800">
</div>
<div class="py-1 content-center">
<h1 class="text-xl">Barend Botje</h1>
<div class="text-sm flex items-center space-x-1">
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor" class="text-green-400 size-4">
<circle cx="12" cy="12" r="9" stroke="currentColor" stroke-width="1"></circle>
</svg>
<div>
Always online
</div>
</div>
</div>
</header>
<main id="conversationResponse" class="p-2 sm:p-4 flex flex-col justify-end text-sm sm:text-base space-y-4 grow">
<div class="bg-gray-50 border border-gray-400 rounded-lg w-full px-2 py-1 text-xs sm:text-sm text-gray-400">
Barend Botje is an idea of Marco Roos and was created by Ivo Fokkema, both at the Leiden University Medical Center in Leiden, the Netherlands.
Conversations will be kept as long as the session is active, which is some 4 hours.
Within that timeframe, the window can be safely refreshed.
Type "#reset" to reset the conversation and start a new one.
</div>
</main>
<div id="barendTyping" class="px-4 sm:px-6 mb-2 text-sm sm:text-base flex items-center space-x-1 text-gray-800 hidden">
<div>
Barend is typing
</div>
<img src="ellipse.gif" class="w-5">
</div>
<footer class="w-full h-40 bg-white border-t border-blue-200 p-2">
<form onsubmit="sendMessage(); return false;" action="">
<div class="pb-2">
<textarea class="w-full h-24 p-2" id="conversationInput" name="conversationInput" placeholder="Talk to me"></textarea>
</div>
<div class="flex justify-end space-x-8">
<button id="conversationDownloadButton" class="px-2 py-1 bg-blue-100 text-gray-600 disabled:opacity-25 rounded-md inline-flex items-center hidden">
Download this conversation
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="animate-spin ml-2 h-5 w-5 hidden">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
</button>
<button type="submit" id="conversationButton" class="px-2 py-1 bg-blue-800 disabled:opacity-25 rounded-md text-white inline-flex items-center">
Send
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="animate-spin ml-2 h-5 w-5 text-white hidden">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
</button>
</div>
</form>
</footer>
</div>
<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
<SCRIPT type="text/javascript">
// Disable the Send button when there is nothing to submit.
$("#conversationInput").keyup(
function (e)
{
if ($(this).val().trim() == '') {
$("#conversationButton").prop('disabled', true);
} else if (e.which == 13) {
$('#conversationButton').click();
} else {
$("#conversationButton").prop('disabled', false);
}
}
).keyup();
// Set the click handlers for the buttons.
// Disable the buttons when clicked, to indicate that the process is loading.
$("#conversationButton").click(
function ()
{
// Disable the button and show it's busy.
$(this).prop('disabled', true).find("svg").removeClass("hidden");
// Disable download button, in case it's shown.
$("#" + this.id.replace('Button', '') + "DownloadButton").prop('disabled', true);
$(this).parents("form").submit();
return false; // Don't submit the form the HTML way.
}
);
$("#conversationDownloadButton").click(
function ()
{
$(this).prop('disabled', true).find("svg").removeClass("hidden");
downloadConversation();
return false; // Don't submit the form.
}
);
function sendMessage (sMessage)
{
// This function sends the data over to the ajax script, puts the message on the screen,
// and displays the response when it comes in.
if (!sMessage) {
var sMessage = $("#conversationInput").val();
}
if (sMessage.length < 1) {
return false;
}
var sMessageBox =
'<div class="rounded-lg w-3/4 px-2 py-1 border">\n' +
' <span></span>\n' +
' <div class="float-right text-xs sm:text-sm text-gray-400 mt-1"></div>\n' +
'</div>';
var oBarend = $.post(
"ajax.php",
{
"input": sMessage,
},
"json"
).always(
function (data)
{
// This code handles the failures as well as the successful calls.
if (typeof(data.data) == "undefined") {
data = {errors: {'ENOJSON': "I don't understand what's happening; I expected to have something to say, but I can't find it."}}
}
if (Object.keys(data.errors).length > 0) {
// This failed. Either completely, or after parsing chatGPT's response.
$("#barendTyping").addClass("hidden");
$("#conversationButton").find("svg").addClass("hidden");
// If the textarea is now empty, put the message back.
if ($("#conversationInput").val() == "" && sMessage != '#init') {
$("#conversationInput").val(sMessage);
$("main").children().last().filter('.self-end').remove();
$("#conversationButton").prop("disabled", false);
}
// Create a new message, make it red, add the text, add the time, append.
var myMessage = $.parseHTML(sMessageBox);
sErrorCode = Object.keys(data.errors)[0];
sErrorMessage = data.errors[sErrorCode];
oNow = new Date(); // This is an error, so use now().
// NOTE: This uses html() rather than text(), and is thus NOT SAFE.
// We use this, so we can apply formatting. Any output that's not trusted should have been escaped by PHP.
$(myMessage).addClass("bg-red-100 border-red-200").find("span").html('(' + sErrorCode + ') ' + sErrorMessage).next("div").text(oNow.getHours() + ":" + oNow.getMinutes().toLocaleString('en-US', {minimumIntegerDigits: 2}));
$("main").append(myMessage);
return false;
}
// We could be asked to reload (when resetting).
if (typeof(data.messages.IRELOAD) != 'undefined') {
location.reload(true);
// This will be shown only by a fraction of a second.
data.data[0] = {
'role': 'assistant',
'content': "<i>(reloading the page...)</i>"
};
}
// Determine delay length - this depends on what the server tells us to do.
nDelay = (typeof(data.messages.INODELAY) == "undefined"? 2000 : 50);
// If we get here, the JSON was already parsed, and we know it was successful.
// We should have received a reply from the chatGPT handler.
if (!data.data.length) {
data.data[0] = {
'role': 'assistant',
'content': "<i>(Barend somehow didn't respond, and just stares at you without saying a word - perhaps he's broken?)</i>"
};
}
// Loop through the messages.
$.each(
data.data,
function (iMessage, oMessage)
{
// All messages will be rendered through setTimeout, so I can time everything.
// This way, it will seem that he takes a while to generate the answers.
setTimeout(
function ()
{
// Create a new message, make it blue, add the text, add the time, append.
var myMessage = $.parseHTML(sMessageBox);
if (oMessage.role == 'assistant') {
$(myMessage).addClass("bg-blue-100 border-blue-200");
} else if (oMessage.role == 'user') {
$(myMessage).addClass("bg-green-200 border-green-300 self-end");
} else {
$(myMessage).addClass("bg-red-100 border-red-200");
}
oNow = new Date(oMessage.created_at * 1000); // This takes UTC input.
$(myMessage).data('created_at', oMessage.created_at); // Store in UTC.
// NOTE: This uses html() rather than text(), and is thus NOT SAFE.
// We use this, so we can apply formatting. Any output that's not trusted should have been escaped by PHP.
$(myMessage).find("span").html(oMessage.content).next("div").text(oNow.getHours() + ":" + oNow.getMinutes().toLocaleString('en-US', {minimumIntegerDigits: 2}));
$("main").append(myMessage);
},
(nDelay * iMessage)
);
}
);
// Clean up, but only when the last reply has been received.
setTimeout(
function ()
{
// Remove the "Barend is typing...";
$("#barendTyping").addClass("hidden");
// Reset the submit button. Still disabled (there's no message), but without the loader.
$("#conversationButton").prop("disabled", true).find("svg").addClass("hidden");
// Enable the download button.
$("#conversationDownloadButton").removeClass("hidden").prop("disabled", false);
},
(nDelay * (data.data.length - 1))
);
return false;
}
);
// When this didn't fail, we can put the message on the screen.
if (oBarend.state() != "rejected") {
if ($("#conversationInput").val()) {
// This if() makes sure that we can call sendMessage("message") without showing the user what we sent.
var myMessage = $.parseHTML(sMessageBox);
oNow = new Date(); // This is the user's message, so use now().
$(myMessage).data('created_at', parseInt(oNow.getTime()/1000)); // Store in UTC.
$(myMessage).addClass("bg-green-200 border-green-300 self-end").find("span").text(sMessage).next("div").text(oNow.getHours() + ":" + oNow.getMinutes().toLocaleString('en-US', {minimumIntegerDigits: 2}));
$("main").append(myMessage);
$("#conversationInput").val("");
}
// Display that Barend is responding, if this is necessary.
setTimeout(
function ()
{
if (oBarend.state() == 'pending') {
$("#barendTyping").removeClass("hidden");
}
},
500
);
}
return false;
}
function downloadConversation ()
{
// Download the result or results into a tab-delimited file.
var aMessages = $("main").children().slice(1); // Skip the intro.
var fileContent = '';
// Loop through the messages and convert them into tab-delimited data.
$.each(
aMessages,
function (index, aMessage)
{
// Collect the body first.
var sBody = $(aMessage).find("span").text();
var d = new Date($(aMessage).data("created_at") * 1000);
fileContent +=
'[' + d.toISOString().replace(".000Z", "Z") + '] ' +
($(aMessage).filter(".self-end").length? 'You' : 'Barend') + ': ' +
sBody.trim() + '\r\n';
}
);
// Use base64 encoding so that Firefox will also create a valid file.
fileContent =
"data:text/plain;base64," + btoa(fileContent + '\r\n');
var link = document.createElement("a");
link.setAttribute("href", fileContent);
var d = new Date();
// Offset the timezone.
d.setMinutes(d.getMinutes() - d.getTimezoneOffset());
link.setAttribute("download", "BarendBotje_" + d.toISOString().slice(0, 19) + ".txt");
document.body.appendChild(link);
link.click();
// Reset button, but only after a second since the download is too fast.
setTimeout(
function ()
{
$("#conversationDownloadButton").prop("disabled", false).find("svg").addClass("hidden");
},
1000
)
// Clean up.
link.remove();
}
$()
{
sendMessage("#init");
}
</SCRIPT>
</body>
</html>