-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (60 loc) · 2.78 KB
/
script.js
File metadata and controls
78 lines (60 loc) · 2.78 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
function el(selector) {
const element = /** @type {HTMLElement} */(document.querySelector(selector));
if (!element) throw new Error(`Element ${selector} not found`);
return element;
}
const btn = el('#btn')
const userInput = el('#userInput')
const p = el('.pa');
const img = el('.img')
const pre = el('.pr')
const delbtn = el('#delbtn')
var data = JSON.parse(localStorage.getItem('userData')) || {
username: 'suijin',
role:'admin',
xp: 0,
level: 0,
past: ['user: Call me suijin'],
}
img.src = `https://coderx-api.onrender.com/v1/canvas/coderx/profile?backgroundURL=https://i.ibb.co.com/2jMjYXK/IMG-20250103-WA0469.jpg&avatarURL=https://avatars.githubusercontent.com/u/159487561?v=4&rankName=${data.role}&rankId=0&requireExp=700&level=1&name=${data.username}&exp=${data.xp}`
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve,ms))
}
async function getData(term) {
const prompts = `${data.past.join("\n\n")} If I ask to learn or be quizzed, start a quiz of up to 10 questions (max 100 XP). Only give XP when a quiz is active. Say XP earned (0 if wrong) after each answer, never state XP per question, and give total XP + feedback at the end. Do not award XP or respond with XP values unless it's part of a quiz. Otherwise, respond normally.`;
const encodedPrompt = encodeURIComponent(prompts);
const response = await fetch(`https://api.siputzx.my.id/api/ai/gpt3?prompt=${encodedPrompt}&content=${term}`);
const datax = await response.json();
p.style.color = 'white'
const message = datax.data
let line = ''
const words = message.split(" ")
for (word of words){
line += word + ' '
p.textContent = line;
await sleep(100)
}
return datax.data ;
}
const textarea = el('#userInput')
textarea.addEventListener('input', () => {
textarea.style.height = 'auto';
textarea.style.height = `${textarea.scrollHeight}px`;
});
delbtn.addEventListener('click',() => {
p.textContent = 'Succesfully cleared history'
localStorage.clear();
console.log(data)
})
btn.addEventListener('click', async function() {
const info = textarea.value;
const aiResponse = await getData(info);
data.past.push(`User: ${info}\nAI: ${aiResponse}`);
console.log(aiResponse)
number = aiResponse.match(/\d+/)[0];
data.xp += Number(number)
localStorage.setItem('userData', JSON.stringify(data));
img.src = `https://coderx-api.onrender.com/v1/canvas/coderx/profile?backgroundURL=https://i.ibb.co.com/2jMjYXK/IMG-20250103-WA0469.jpg&avatarURL=https://avatars.githubusercontent.com/u/159487561?v=4&rankName=${data.role}&rankId=0&requireExp=700&level=1&name=${data.username}&exp=${data.xp}`;
console.log(data);
});
textarea.value = ''