This repository was archived by the owner on Nov 20, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathgpt.js
More file actions
435 lines (411 loc) · 19.3 KB
/
Copy pathgpt.js
File metadata and controls
435 lines (411 loc) · 19.3 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import { coachStream, getSession, getMessages, TimeoutError, InvalidArgumentError } from "/scholarly_api_latest.js"
var atBottom = true;
var ready = false;
var index = 0;
var imageIndex = 0;
var running = false;
var readyToStop = false;
var controller = null;
var settings = false;
var saveChats = undefined;
var profile = undefined;
var newProfile = undefined;
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
function copySession() {
navigator.clipboard.writeText(session).then(
() => {
alert("Copied Session ID: "+session)
},
() => {
alert("Failed to copy session. Error: No permission")
},
);
}
function updateSettings() {
saveChats = document.querySelector('#save-check').checked;
if(!saveChats) document.cookie = "session=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
document.cookie = "save-chats="+saveChats+";";
var select = document.querySelector('select');
newProfile = select.options[select.selectedIndex].value;
let profileText = profile.charAt(0).toUpperCase() + profile.slice(1);
let selectedProfileText = newProfile.charAt(0).toUpperCase() + newProfile.slice(1);
if (profile == newProfile) document.querySelector('#profile-tip-text').textContent = "Currently selected profile: "+profileText;
else document.querySelector('#profile-tip-text').textContent = "Reset chat to switch from "+profileText+" to "+selectedProfileText;
}
function updateImage() {
if (document.getElementById("upload").files.length == 0) { document.getElementById("image-icon").innerHTML = '<i class="fa-solid fa-image"></i>' }
else { document.getElementById("image-icon").innerHTML = '<i class="fa-solid fa-trash-can"></i>' }
}
setInterval(updateSettings, 100);
setInterval(updateImage, 100);
function settingsButton() {
if (ready === false) return;
if(!settings) {
document.querySelector('.settings-container').style.display = "flex";
document.querySelector('#settings').querySelector('i').classList.remove('fa-wrench');
document.querySelector('#settings').querySelector('i').classList.add('fa-times');
}
else {
document.querySelector('.settings-container').style.display = "none";
document.querySelector('#settings').querySelector('i').classList.remove('fa-times');
document.querySelector('#settings').querySelector('i').classList.add('fa-wrench');
}
settings = !settings;
}
function resetButton() {
if (ready === false) return;
else clearSessionCookie();
}
function uploadImage() {
if (false) {
alert("This profile cannot upload images. Change your profile in settings.");
event.preventDefault();
}
else {
if (document.getElementById("upload").files.length != 0) {
if(confirm('Would you like to remove the uploaded image?')) {
document.getElementById("upload").value = '';
}
event.preventDefault();
}
}
}
function clearSessionCookie() {
if (confirm("Do you want to reset this chat?") == true) {
if (newProfile == profile) document.cookie = "profile=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
else document.cookie = "profile="+newProfile+";";
document.cookie = "session=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
location.reload();
}
}
async function getSessionCookie() {
let select = document.querySelector('select');
profile = getCookie('profile');
if (profile === undefined) profile = 'default';
select.value = profile;
saveChats = getCookie("save-chats");
if(saveChats === "false") {
document.querySelector('#save-check').checked = false;
document.cookie = "session=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
let session = await getSession(profile, 5000);
return session
}
if(saveChats === undefined) {
document.cookie = "save-chats=true;";
saveChats = true;
document.querySelector('#save-check').checked = true;
}
if (getCookie("session") == undefined) {
try {
let session = await getSession(profile, 5000);
document.cookie = "session="+session+";";
return getCookie('session');
} catch (error) {
if (error instanceof TimeoutError) {
return undefined;
}
}
}
else {
var cookieSession = getCookie("session");
try {
const messages = await getMessages(cookieSession, 5000);
messages.forEach(function(item) {
let image = item.content.length === 2;
if (item.role == "user") {
const meItem = document.createElement("li");
if (image) {
imageIndex += 1;
meItem.innerHTML = `
<div class="coach-feature-card1">
<svg viewBox="0 0 731.4285714285713 1024" class="coach-icon02">
<path
d="M731.429 799.429c0 83.429-54.857 151.429-121.714 151.429h-488c-66.857 0-121.714-68-121.714-151.429 0-150.286 37.143-324 186.857-324 46.286 45.143 109.143 73.143 178.857 73.143s132.571-28 178.857-73.143c149.714 0 186.857 173.714 186.857 324zM585.143 292.571c0 121.143-98.286 219.429-219.429 219.429s-219.429-98.286-219.429-219.429 98.286-219.429 219.429-219.429 219.429 98.286 219.429 219.429z"
></path>
</svg>
<div class="coach-container2">
<h2 class="coach-text14">
<span>Me</span>
<br />
</h2>
<button id='image${imageIndex.toString()}' data-file="${imageIndex.toString()}" class="button-secondary button button-md input-field-register" style="padding: 12px; margin-bottom: 12px; margin-top: -10px;">
<span><i class="fa-solid fa-image"></i> Image Attached</span>
</button>
<span style="white-space: pre-wrap;"><pre>${item.content[1].text}</pre></span>
</div>
</div>
`;
document.getElementById("the_list").appendChild(meItem);
let id = '#image'+imageIndex.toString();
document.querySelector(id).addEventListener('click', openImage);
}
else {
meItem.innerHTML = `
<div class="coach-feature-card1">
<svg viewBox="0 0 731.4285714285713 1024" class="coach-icon02">
<path
d="M731.429 799.429c0 83.429-54.857 151.429-121.714 151.429h-488c-66.857 0-121.714-68-121.714-151.429 0-150.286 37.143-324 186.857-324 46.286 45.143 109.143 73.143 178.857 73.143s132.571-28 178.857-73.143c149.714 0 186.857 173.714 186.857 324zM585.143 292.571c0 121.143-98.286 219.429-219.429 219.429s-219.429-98.286-219.429-219.429 98.286-219.429 219.429-219.429 219.429 98.286 219.429 219.429z"
></path>
</svg>
<div class="coach-container2">
<h2 class="coach-text14">
<span>Me</span>
<br />
</h2>
<span style="white-space: pre-wrap;"><pre>${item.content[0].text}</pre></span>
</div>
</div>
`;
document.getElementById("the_list").appendChild(meItem);
}
}
else if (item.role == "assistant") {
index += 1;
const messageItem = document.createElement("li");
messageItem.innerHTML = `
<div class="coach-feature-card">
<svg viewBox="0 0 1316.5714285714284 1024" class="coach-icon">
<path d="M1013.714 477.714l10.286 180.571c4.571 80.571-164 146.286-365.714 146.286s-370.286-65.714-365.714-146.286l10.286-180.571 328 103.429c9.143 2.857 18.286 4 27.429 4s18.286-1.143 27.429-4zM1316.571 292.571c0 8-5.143 14.857-12.571 17.714l-640 201.143c-2.286 0.571-4 0.571-5.714 0.571s-3.429 0-5.714-0.571l-372.571-117.714c-32.571 25.714-55.429 88.571-60 165.714 21.714 12.571 36 35.429 36 62.286 0 25.714-13.143 48-33.143 61.143l33.143 247.429c0.571 5.143-1.143 10.286-4.571 14.286s-8.571 6.286-13.714 6.286h-109.714c-5.143 0-10.286-2.286-13.714-6.286s-5.143-9.143-4.571-14.286l33.143-247.429c-20-13.143-33.143-35.429-33.143-61.143 0-27.429 15.429-50.857 37.143-63.429 3.429-66.857 20.571-138.857 56-188.571l-190.286-59.429c-7.429-2.857-12.571-9.714-12.571-17.714s5.143-14.857 12.571-17.714l640-201.143c2.286-0.571 4-0.571 5.714-0.571s3.429 0 5.714 0.571l640 201.143c7.429 2.857 12.571 9.714 12.571 17.714z"></path>
</svg>
<div class="coach-container1">
<h2 class="coach-text10">
<span>Coach</span>
<br />
</h2>
<span style="white-space: pre-wrap;"><pre id=textbox>${item.content}</pre></span>
</div>
</div>
`;
document.getElementById("the_list").appendChild(messageItem);
}
});
window.scrollTo(0, document.body.scrollHeight);
return cookieSession;
} catch (error) {
if (error instanceof TimeoutError) {
return undefined;
}
else if (error instanceof InvalidArgumentError) {
return "outdated";
}
}
}
}
var session = await getSessionCookie();
if (session != "outdated" && session != undefined) {
document.querySelector('#session-text').textContent = "ID: "+session;
ready = true;
document.querySelector('#button-text').innerHTML = '<i class="fa-solid fa-paper-plane"></i>';
}
else if (session === "outdated") {
document.cookie = "session=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
document.cookie = "profile=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
location.reload();
}
else if (session == undefined) {
let elemDiv = document.createElement('div');
elemDiv.innerHTML = `
<div class="adblock-container">
<div class="adblock-heading-container">
<svg viewBox="0 0 1024 1024" class="adblock-icon">
<path
d="M554 554v-256h-84v256h84zM554 726v-86h-84v86h84zM512 86q176 0 301 125t125 301-125 301-301 125-301-125-125-301 125-301 301-125z"
></path>
</svg>
<h1 class="adblock-text">
<span><span>Failed to Connect</span></span>
<br />
</h1>
<span class="adblock-text03">
<span>
<span style="white-space: pre-wrap;">Coach is in early access, and we cannot connect to our servers. Apologies for the inconvienience as we try to bring Coach back online.
</span>
</span>
<br />
<span></span>
<br />
<span></span>
<br />
</span>
</div>
</div>
`;
let loc = document.getElementById('adblock_location');
loc.appendChild(elemDiv);
}
window.addEventListener('scroll', function() {
if (document.documentElement.scrollTop + window.innerHeight > document.documentElement.scrollHeight - 60) {
atBottom = true;
}
else {
atBottom = false;
}
});
document.querySelector('#button').addEventListener('click', handleButtonClick);
document.querySelector('#settings').addEventListener('click', settingsButton);
document.querySelector('#reset').addEventListener('click', resetButton);
document.querySelector('#copy').addEventListener('click', copySession);
document.querySelector('#upload').addEventListener('click', uploadImage);
document.addEventListener("keydown", function(event) {
if (event.code === "Enter" && !event.shiftKey && !event.ctrlKey && !event.altKey) {
event.preventDefault();
handleEnter();
}
});
function openImage() {
if (this.innerText != ' Image Attached') return;
window.open("https://legacy.scholarly.digital/v1/image/"+session+"/"+this.dataset.file);
}
function delay(milliseconds) {
return new Promise(resolve => {
setTimeout(resolve, milliseconds);
});
}
function handleButtonClick() {
if (running && readyToStop && ready) {
controller.abort();
}
else if (!running && !readyToStop && ready) {
Send();
}
}
function handleEnter() {
if (!running && !readyToStop) {
Send();
}
}
async function Send() {
var sessionToken = session;
var sQuestion = promptinput.value;
var image = null
if (sQuestion == "") {
return;
}
if(settings) settingsButton();
if(true) { if (document.getElementById("upload").files.length === 1) {
image = document.getElementById("upload").files[0];
imageIndex += 1;
} }
document.querySelector('#button-text').innerHTML = "<i class='fa-solid fa-gear fa-spin'></i>";
running = true;
index += 1;
const meItem = document.createElement("li");
if (!image) {
meItem.innerHTML = `
<div class="coach-feature-card1">
<svg viewBox="0 0 731.4285714285713 1024" class="coach-icon02">
<path
d="M731.429 799.429c0 83.429-54.857 151.429-121.714 151.429h-488c-66.857 0-121.714-68-121.714-151.429 0-150.286 37.143-324 186.857-324 46.286 45.143 109.143 73.143 178.857 73.143s132.571-28 178.857-73.143c149.714 0 186.857 173.714 186.857 324zM585.143 292.571c0 121.143-98.286 219.429-219.429 219.429s-219.429-98.286-219.429-219.429 98.286-219.429 219.429-219.429 219.429 98.286 219.429 219.429z"
></path>
</svg>
<div class="coach-container2">
<h2 class="coach-text14">
<span>Me</span>
<br />
</h2>
<span style="white-space: pre-wrap;"><pre>${sQuestion}</pre></span>
</div>
</div>
`;
document.getElementById("the_list").appendChild(meItem);
}
else {
meItem.innerHTML = `
<div class="coach-feature-card1">
<svg viewBox="0 0 731.4285714285713 1024" class="coach-icon02">
<path
d="M731.429 799.429c0 83.429-54.857 151.429-121.714 151.429h-488c-66.857 0-121.714-68-121.714-151.429 0-150.286 37.143-324 186.857-324 46.286 45.143 109.143 73.143 178.857 73.143s132.571-28 178.857-73.143c149.714 0 186.857 173.714 186.857 324zM585.143 292.571c0 121.143-98.286 219.429-219.429 219.429s-219.429-98.286-219.429-219.429 98.286-219.429 219.429-219.429 219.429 98.286 219.429 219.429z"
></path>
</svg>
<div class="coach-container2">
<h2 class="coach-text14">
<span>Me</span>
<br />
</h2>
<button id='image${imageIndex.toString()}' data-file="${imageIndex.toString()}" class="button-secondary button button-md input-field-register" style="padding: 12px; margin-bottom: 12px; margin-top: -10px;">
<span><i class="fa-solid fa-gear fa-spin"></i> Uploading Image</span>
</button>
<span style="white-space: pre-wrap;"><pre>${sQuestion}</pre></span>
</div>
</div>
`;
document.getElementById("the_list").appendChild(meItem);
let id = '#image'+imageIndex.toString();
document.querySelector(id).addEventListener('click', openImage);
}
window.scrollTo(0, document.body.scrollHeight);
promptinput.value = "";
await delay(1000);
const messageItem = document.createElement("li");
messageItem.innerHTML = `
<div class="coach-feature-card">
<svg viewBox="0 0 1316.5714285714284 1024" class="coach-icon">
<path d="M1013.714 477.714l10.286 180.571c4.571 80.571-164 146.286-365.714 146.286s-370.286-65.714-365.714-146.286l10.286-180.571 328 103.429c9.143 2.857 18.286 4 27.429 4s18.286-1.143 27.429-4zM1316.571 292.571c0 8-5.143 14.857-12.571 17.714l-640 201.143c-2.286 0.571-4 0.571-5.714 0.571s-3.429 0-5.714-0.571l-372.571-117.714c-32.571 25.714-55.429 88.571-60 165.714 21.714 12.571 36 35.429 36 62.286 0 25.714-13.143 48-33.143 61.143l33.143 247.429c0.571 5.143-1.143 10.286-4.571 14.286s-8.571 6.286-13.714 6.286h-109.714c-5.143 0-10.286-2.286-13.714-6.286s-5.143-9.143-4.571-14.286l33.143-247.429c-20-13.143-33.143-35.429-33.143-61.143 0-27.429 15.429-50.857 37.143-63.429 3.429-66.857 20.571-138.857 56-188.571l-190.286-59.429c-7.429-2.857-12.571-9.714-12.571-17.714s5.143-14.857 12.571-17.714l640-201.143c2.286-0.571 4-0.571 5.714-0.571s3.429 0 5.714 0.571l640 201.143c7.429 2.857 12.571 9.714 12.571 17.714z"></path>
</svg>
<div class="coach-container1">
<h2 class="coach-text10">
<span>Coach</span>
<br />
</h2>
<span style="white-space: pre-wrap;"><pre id=textbox${index}>Thinking...</pre></span>
</div>
</div>
`;
document.getElementById("the_list").appendChild(messageItem);
window.scrollTo(0, document.body.scrollHeight);
var currentContent = "";
controller = new AbortController();
const onContentReceived = (content) => {
if (document.getElementById(("textbox" + index.toString())).innerHTML == "Thinking...") {
if (image !== null) {
let id = '#image'+imageIndex.toString();
document.getElementById("upload").value = '';
document.querySelector(id).innerHTML = '<span><i class="fa-solid fa-image"></i> Image Attached</span>';
}
window.scrollTo(0, document.body.scrollHeight);
document.getElementById(("textbox" + index.toString())).innerHTML = "";
document.querySelector('#button-text').innerHTML = '<i class="fa-solid fa-circle-stop"></i>';
readyToStop = true;
}
currentContent += content
if (atBottom) { window.scrollTo(0, document.body.scrollHeight); }
document.getElementById(("textbox" + index.toString())).innerHTML = currentContent;
};
const onError = (err) => {
if (document.getElementById(("textbox" + index.toString())).innerHTML == "Thinking...") {
if (image !== null) {
let id = '#image'+imageIndex.toString();
document.querySelector(id).innerHTML = '<span><i class="fa-solid fa-circle-exclamation"></i> Image Failed</span>';
document.getElementById("upload").value = '';
}
}
if (atBottom) { window.scrollTo(0, document.body.scrollHeight); }
if (err.message === "Error: 403") {
document.getElementById(("textbox" + index.toString())).innerHTML = "Sorry but we faced an error. Someone else is currently using with the same chat session. Maybe start a new one witht the reset button on the bottom left?";
currentContent = "Sorry but we faced an error. Someone else is currently using with the same chat session. Maybe start a new one with the reset button on the bottom left?";
}
else if (err.message === "Error: 400") {
document.getElementById(("textbox" + index.toString())).innerHTML = "Sorry but we faced an error. The chat session you were using has disappeared on our end. Maybe start a new one with the reset button on the bottom left?";
currentContent = "Sorry but we faced an error. The chat session you were using has disappeared on our end. Maybe start a new one with the reset button on the bottom left?";
}
else {
document.getElementById(("textbox" + index.toString())).innerHTML = "Sorry but we faced an error. Maybe start a new chat with the reset button on the bottom left. If you want to contact a developer, here is some extra information: " + err;
currentContent = "Sorry but we faced an error. Maybe start a new chat with the reset button on the bottom left. If you want to contact a developer, here is some extra information: " + err.message;
}
}
const onStreamFinished = (manual) => {
if (manual === true) {
currentContent += "[STOPPED BY USER]"
document.getElementById(("textbox" + index.toString())).innerHTML = currentContent;
}
running = false;
readyToStop = false;
document.querySelector('#button-text').innerHTML = '<i class="fa-solid fa-paper-plane"></i>';
window.scrollTo(0, document.body.scrollHeight);
};
coachStream(sQuestion, sessionToken, image, onContentReceived, onStreamFinished, onError, controller.signal);
}