/
j.js
179 lines (155 loc) · 5 KB
/
j.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
'use strict';
let apiKey;
let autoScroll = true;
const decoder = new TextDecoder();
const messagesHistory = []; messagesHistory.limit = 30;
const [errorDiv, chatContainer, chatForm, chatInput] = getElementsById(['error', 'chatContainer', 'chatForm', 'chatInput']);
browser.storage.local.get('chatGptKey').then(storage => apiKey = storage.chatGptKey);
chatContainer.addEventListener('scroll', function autoScrollWhenFullyBottomScrolled() {
autoScroll = (this.scrollHeight - this.scrollTop) === this.clientHeight;
});
chatForm.addEventListener('submit', submitForm);
chatInput.addEventListener('input', changeInputHeight);
chatInput.addEventListener('keydown',
ev => ev.key === 'Enter' && (ev.preventDefault(), ev.target.form.requestSubmit())
);
async function submitForm(e) {
e.preventDefault();
if (renderReply.rendering) return;
if (!apiKey) if (!await promptForKey()) return;
const textContent = e.target.text.value.trim();
if (!textContent.length) return;
updateChatHistory({ role: 'user', textContent });
await renderReply().catch(handleError);
updateChatHistory({ role: 'assistant' });
}
function promptForKey() {
if (document.getElementById('modal')) {
return false;
}
return new Promise(resolve => waitModalConfirmation(resolve));
}
async function renderReply() {
renderReply.rendering = true;
const response = await fetchAPI()
.catch(handleError).finally(() => renderReply.rendering = false);
const reader = response.body.getReader();
const responseMsg = CreateChatMessage('assistant');
chatInput.value = '';
while (true) {
const { value, done } = await reader.read();
if (done) break;
const parsedString = parseResponse(value);
for (const char of parsedString) {
responseMsg.innerText += char ?? '';
if (autoScroll) scrollChatDown();
}
}
}
function parseResponse(value) {
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
return lines.reduce((acc, curr) => {
curr = curr.replace(/^data: /, '').trim();
if (!curr) return acc;
try {
const line = JSON.parse(curr);
const textContent = line.choices[0].delta.content;
if (!textContent) return acc;
acc += textContent;
} catch {} finally { return acc;}
}, '');
}
function waitModalConfirmation(resolve) {
document.body.insertAdjacentHTML('afterbegin',
`<div id='modal'>
<div>
You need API
<a style='font-weight: bolder' target='_blank' href='https://platform.openai.com/account/api-keys'>key</a>
to make requests to ChatGPT API
</div>
<input id='apiKeyInput' placeholder='API key' autofocus />
<button id='modalSubmitBtn'>Save</button>
</div>`
);
document.getElementById('modalSubmitBtn').addEventListener('click', function () {
apiKey = document.getElementById('apiKeyInput').value?.trim();
if (!apiKey) return;
browser.storage.local.set({ 'chatGptKey': apiKey });
resolve(true);
this.closest('#modal').remove();
});
}
function updateChatHistory({ role, textContent = undefined }) {
if (messagesHistory.length > messagesHistory.limit) {
messagesHistory.shift();
}
switch (role) {
case 'user':
const questionMsg = CreateChatMessage('user');
questionMsg.innerText = textContent + '\n';
messagesHistory.push({ role, content: textContent, });
break;
case 'assistant':
const lastQuestion = Array.from(document.querySelectorAll('.response')).pop().innerText;
messagesHistory.push({ role, content: lastQuestion ?? '', },);
break;
}
}
async function fetchAPI() {
return fetch('https://api.openai.com/v1/chat/completions', {
'headers': {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
'body': JSON.stringify({
'model': 'gpt-3.5-turbo',
'messages': messagesHistory,
'stream': true
}),
'method': 'POST',
}).then(r => {
if (!r.ok) throw new Error(r.status.toString());
return r;
});
}
function handleError(err) {
let errorText = 'An error occured';
switch (err.message) {
case '401':
errorText = 'Invalid API key';
browser.storage.local.remove('chatGptKey');
apiKey = null;
break;
case '400':
errorText = 'Server error';
break;
case '429':
errorText = 'Too many requests or quota exceeded';
}
errorDiv.innerText = errorText;
messagesHistory.pop();
setTimeout(() => errorDiv.innerText = '', 6000);
throw err;
}
function CreateChatMessage(type) {
const element = document.createElement('div');
element.className = type === 'user'
? 'question'
: 'response';
chatContainer.append(element);
return element;
}
function changeInputHeight() {
this.style.height = '';
this.style.height = this.scrollHeight + 'px';
scrollChatDown();
}
function getElementsById(idList = []) {
return idList.reduce((acc, id) =>
[...acc, document.getElementById(id)],
[]).filter(Boolean);
}
function scrollChatDown() {
chatContainer.scrollTop = chatContainer.scrollHeight;
}