-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatbot.js
397 lines (338 loc) · 14.7 KB
/
chatbot.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
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
document.addEventListener('DOMContentLoaded', () => {
const chatbox = document.getElementById('chatbox');
const userInput = document.getElementById('userInput');
const sendButton = document.getElementById('sendButton');
const apiKeyInput = document.getElementById('apiKeyInput');
const themeToggle = document.getElementById('themeToggle');
const generateAudioButton = document.getElementById('generateAudioButton');
const audioLinkContainer = document.getElementById('audioLinkContainer');
const audioControls = document.getElementById('audioControls');
const progressBar = document.getElementById('progress');
const playPauseButton = document.getElementById('playPauseButton');
const stopButton = document.getElementById('stopButton');
const restartButton = document.getElementById('restartButton');
const urlParams = new URLSearchParams(window.location.search);
const summary = urlParams.get('summary');
const url = urlParams.get('url');
let conversationHistory = [];
let audioBlob = null;
let audioElement = null;
let isCancelled = false;
// Load API key from storage
chrome.storage.local.get(['apiKey'], function(result) {
if (result.apiKey) {
apiKeyInput.value = result.apiKey;
}
});
// Save API key when it changes
apiKeyInput.addEventListener('change', function() {
const apiKey = apiKeyInput.value.trim();
chrome.storage.local.set({apiKey: apiKey}, function() {
console.log('API key saved');
});
});
// Theme toggle functionality
themeToggle.addEventListener('click', () => {
document.documentElement.setAttribute('data-theme',
document.documentElement.getAttribute('data-theme') === 'light' ? 'dark' : 'light'
);
});
// Initialize chat with summary
if (summary) {
const initialMessage = `Here's a summary of the content from ${url}:\n\n${summary}\n\nHow can I assist you with this content?`;
appendMessage('Bot', initialMessage, 'bot-message');
conversationHistory.push({role: "model", parts: [{text: initialMessage}]});
} else {
appendMessage('Bot', `Welcome! I'm here to chat about the content from: ${url}. How can I assist you?`, 'bot-message');
}
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
generateAudioButton.addEventListener('click', generateAudio);
generateAudioButton.addEventListener('click', handleGenerateAudio);
playPauseButton.addEventListener('click', togglePlayPause);
stopButton.addEventListener('click', stopAudio);
restartButton.addEventListener('click', restartAudio);
async function sendMessage() {
const message = userInput.value.trim();
const apiKey = apiKeyInput.value.trim();
if (message && apiKey) {
appendMessage('You', message, 'user-message');
userInput.value = '';
conversationHistory.push({role: "user", parts: [{text: message}]});
try {
appendMessage('Bot', 'Thinking...', 'bot-message');
const response = await generateResponse(apiKey, conversationHistory);
// Remove the "Thinking..." message
chatbox.removeChild(chatbox.lastChild);
appendMessage('Bot', response, 'bot-message');
conversationHistory.push({role: "model", parts: [{text: response}]});
} catch (error) {
console.error('Error generating response:', error);
// Remove the "Thinking..." message
chatbox.removeChild(chatbox.lastChild);
appendMessage('Bot', `Sorry, I encountered an error: ${error.message}`, 'bot-message');
}
} else if (!apiKey) {
alert("Please enter your Google API key.");
}
}
async function handleGenerateAudio() {
if (generateAudioButton.textContent === 'Generate Podcast') {
isCancelled = false;
generateAudioButton.textContent = 'Cancel Generation';
audioLinkContainer.innerHTML = `
<div id="progressBarContainer" style="width: 100%; background-color: #ddd; margin-bottom: 10px;">
<div id="progressBar" style="width: 0%; height: 20px; background-color: #4CAF50;"></div>
</div>
<div id="progressText">Generating podcast... 0% (Estimated time: calculating...)</div>
`;
try {
await generateAudio();
} catch (error) {
if (!isCancelled) {
console.error('Error generating audio:', error);
audioLinkContainer.textContent = 'Error generating audio. Please try again.';
}
}
generateAudioButton.textContent = 'Generate Podcast';
} else {
isCancelled = true;
generateAudioButton.textContent = 'Generate Podcast';
audioLinkContainer.textContent = 'Generation cancelled.';
}
}
async function generateAudio() {
const fullText = conversationHistory.map(entry =>
`${entry.role === 'user' ? 'You' : 'Bot'}: ${entry.parts[0].text}`
).join('\n\n');
const totalChars = fullText.length;
const charsPerSecond = 15; // Adjust this based on your observed speech rate
const estimatedTotalSeconds = totalChars / charsPerSecond;
const utterance = new SpeechSynthesisUtterance(fullText);
let startTime;
let processedChars = 0;
utterance.onstart = () => {
startTime = Date.now();
};
utterance.onboundary = (event) => {
if (isCancelled) {
speechSynthesis.cancel();
throw new Error('Generation cancelled');
}
processedChars = event.charIndex;
const elapsedSeconds = (Date.now() - startTime) / 1000;
const progress = (processedChars / totalChars) * 100;
const remainingSeconds = estimatedTotalSeconds - elapsedSeconds;
updateProgress(progress, remainingSeconds);
};
await new Promise((resolve, reject) => {
utterance.onend = resolve;
utterance.onerror = reject;
speechSynthesis.speak(utterance);
});
if (isCancelled) throw new Error('Generation cancelled');
// Here you would implement the logic to convert the speech to an audio file
// For demonstration, we'll create a dummy audio blob
const audioBlob = new Blob([new ArrayBuffer(1000)], { type: 'audio/mp3' });
const audioUrl = URL.createObjectURL(audioBlob);
const downloadLink = document.createElement('a');
downloadLink.href = audioUrl;
downloadLink.download = 'chat_podcast.mp3';
downloadLink.textContent = 'Download Podcast (MP3)';
audioLinkContainer.innerHTML = '';
audioLinkContainer.appendChild(downloadLink);
}
function updateProgress(progress, remainingSeconds) {
const progressBar = document.getElementById('progressBar');
const progressText = document.getElementById('progressText');
progressBar.style.width = `${progress}%`;
const minutes = Math.floor(remainingSeconds / 60);
const seconds = Math.floor(remainingSeconds % 60);
const timeString = `${minutes}:${seconds.toString().padStart(2, '0')}`;
progressText.textContent = `Generating podcast... ${Math.round(progress)}% (Estimated time remaining: ${timeString})`;
}
function textToAudioBuffer(audioContext, utterance) {
return new Promise((resolve) => {
const audioChunks = [];
const sampleRate = audioContext.sampleRate;
const scriptProcessor = audioContext.createScriptProcessor(4096, 1, 1);
scriptProcessor.onaudioprocess = (event) => {
const channel = event.outputBuffer.getChannelData(0);
audioChunks.push(new Float32Array(channel));
};
utterance.onend = () => {
scriptProcessor.disconnect();
const totalLength = audioChunks.reduce((sum, chunk) => sum + chunk.length, 0);
const audioBuffer = audioContext.createBuffer(1, totalLength, sampleRate);
let offset = 0;
for (const chunk of audioChunks) {
audioBuffer.copyToChannel(chunk, 0, offset);
offset += chunk.length;
}
resolve(audioBuffer);
};
const dummySource = audioContext.createBufferSource();
dummySource.connect(scriptProcessor).connect(audioContext.destination);
dummySource.start();
speechSynthesis.speak(utterance);
});
}
function concatenateAudioBuffers(audioContext, buffers) {
const totalLength = buffers.reduce((sum, buffer) => sum + buffer.length, 0);
const finalBuffer = audioContext.createBuffer(
1,
totalLength,
audioContext.sampleRate
);
let offset = 0;
for (const buffer of buffers) {
finalBuffer.copyToChannel(buffer.getChannelData(0), 0, offset);
offset += buffer.length;
}
return finalBuffer;
}
function audioBufferToWav(buffer) {
const numOfChan = buffer.numberOfChannels;
const length = buffer.length * numOfChan * 2 + 44;
const arrayBuffer = new ArrayBuffer(length);
const view = new DataView(arrayBuffer);
const channels = [];
let sample;
let offset = 0;
let pos = 0;
// Write WAV header
setUint32(0x46464952); // "RIFF"
setUint32(length - 8); // file length - 8
setUint32(0x45564157); // "WAVE"
setUint32(0x20746d66); // "fmt " chunk
setUint32(16); // length = 16
setUint16(1); // PCM (uncompressed)
setUint16(numOfChan);
setUint32(buffer.sampleRate);
setUint32(buffer.sampleRate * 2 * numOfChan); // avg. bytes/sec
setUint16(numOfChan * 2); // block-align
setUint16(16); // 16-bit
setUint32(0x61746164); // "data" chunk
setUint32(length - pos - 4); // chunk length
for (let i = 0; i < buffer.numberOfChannels; i++) {
channels.push(buffer.getChannelData(i));
}
while (pos < length) {
for (let i = 0; i < numOfChan; i++) {
sample = Math.max(-1, Math.min(1, channels[i][offset]));
sample = (0.5 + sample < 0 ? sample * 32768 : sample * 32767) | 0;
view.setInt16(pos, sample, true);
pos += 2;
}
offset++;
}
return new Blob([arrayBuffer], { type: "audio/wav" });
function setUint16(data) {
view.setUint16(pos, data, true);
pos += 2;
}
function setUint32(data) {
view.setUint32(pos, data, true);
pos += 4;
}
}
function togglePlayPause() {
if (audioElement) {
if (audioElement.paused) {
audioElement.play();
playPauseButton.textContent = 'Pause';
} else {
audioElement.pause();
playPauseButton.textContent = 'Play';
}
}
}
function stopAudio() {
if (audioElement) {
audioElement.pause();
audioElement.currentTime = 0;
playPauseButton.textContent = 'Play';
updateProgressBar();
}
}
function restartAudio() {
if (audioElement) {
audioElement.currentTime = 0;
audioElement.play();
playPauseButton.textContent = 'Pause';
}
}
function updateProgressBar() {
if (audioElement) {
const progress = (audioElement.currentTime / audioElement.duration) * 100;
progressBar.style.width = `${progress}%`;
}
}
function resetAudio() {
playPauseButton.textContent = 'Play';
progressBar.style.width = '0%';
}
function appendMessage(sender, message, className) {
const messageElement = document.createElement('div');
messageElement.className = `message ${className}`;
const senderElement = document.createElement('strong');
senderElement.textContent = `${sender}:`;
messageElement.appendChild(senderElement);
const contentElement = document.createElement('div');
contentElement.className = 'message-content';
contentElement.innerHTML = marked.parse(message);
messageElement.appendChild(contentElement);
chatbox.appendChild(messageElement);
chatbox.scrollTop = chatbox.scrollHeight;
// Apply syntax highlighting to code blocks
messageElement.querySelectorAll('pre code').forEach((block) => {
Prism.highlightElement(block);
});
}
async function generateResponse(apiKey, history) {
const apiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent';
console.log('Sending request to Gemini Pro API...');
console.log('Conversation history:', history);
const response = await fetch(`${apiUrl}?key=${apiKey}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
contents: history,
generationConfig: {
temperature: 0.7,
topK: 40,
topP: 0.95,
maxOutputTokens: 1000,
},
}),
});
if (!response.ok) {
const errorText = await response.text();
console.error('API response:', response.status, errorText);
throw new Error(`HTTP error! status: ${response.status}. ${errorText}`);
}
const data = await response.json();
console.log('API response data:', data);
if (!data.candidates || data.candidates.length === 0) {
throw new Error('No response generated by the model.');
}
return data.candidates[0].content.parts[0].text;
}
// Configure marked options
marked.setOptions({
breaks: true,
gfm: true,
highlight: function (code, lang) {
if (Prism.languages[lang]) {
return Prism.highlight(code, Prism.languages[lang], lang);
}
return code;
}
});
});