-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
345 lines (291 loc) · 11.3 KB
/
renderer.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
let globalBase64Image = ''
let mediaSource;
let sourceBuffer;
let audioElement = document.createElement('audio');
let currentResponseDiv = null;
initializeMediaSource();
document.getElementById('toggle-dark-mode').addEventListener('click', async () => {
const isDarkMode = await window.darkMode.toggle()
document.getElementById('theme-source').innerHTML = isDarkMode ? 'Dark' : 'Light'
})
document.getElementById('send-prompt').addEventListener('click', () => {
const promptInput = document.getElementById('prompt-input');
let prompt = promptInput.value;
appendUserPrompt(prompt);
currentResponseDiv = createResponseDiv();
promptInput.value = '';
window.electronAPI.send('send-prompt', { prompt, base64Image: globalBase64Image });
playAudio();
});
// In renderer.js
const promptInput = document.getElementById('prompt-input');
promptInput.addEventListener('input', () => {
window.electronAPI.send('update-prompt-text', promptInput.value);
});
promptInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
// Prevent the default action to avoid submitting a form, if any
event.preventDefault();
// Get the prompt text from the input
const promptInput = document.getElementById('prompt-input');
let prompt = promptInput.value;
appendUserPrompt(prompt);
currentResponseDiv = createResponseDiv();
// Check if the prompt is not empty
if (prompt.trim() !== '') {
console.log("Sending Prompt:", prompt);
window.electronAPI.send('send-prompt', { prompt, base64Image: globalBase64Image });
promptInput.value = '';
}
}
});
function appendUserPrompt(prompt) {
const chatElement = document.getElementById('response-container');
// Create and append the user prompt
const promptDiv = document.createElement('div');
promptDiv.classList.add('user-prompt');
promptDiv.textContent = prompt;
chatElement.appendChild(promptDiv);
chatElement.scrollTop = chatElement.scrollHeight;
}
function createResponseDiv() {
const chatElement = document.getElementById('response-container');
const responseDiv = document.createElement('div');
responseDiv.classList.add('api-response');
chatElement.appendChild(responseDiv);
chatElement.scrollTop = chatElement.scrollHeight;
return responseDiv;
}
function appendToResponseDiv(div, response) {
// Append the new chunk of response
div.textContent += response;
const chatElement = document.getElementById('response-container');
chatElement.scrollTop = chatElement.scrollHeight;
}
let currentAudio = null;
let currentAudioFilePath = '';
// // window.electronAPI.receive('response-received', ({ text, audioFilePath }) => {
// // currentAudioFilePath = audioFilePath;
// // if (currentAudio) {
// // currentAudio.pause();
// // }
// // currentAudio = new Audio(currentAudioFilePath);
// // currentAudio.play().catch(e => console.error('Error playing audio:', e));
// // });
window.electronAPI.receive('streamed-response', (partialResponse) => {
if (currentResponseDiv) {
appendToResponseDiv(currentResponseDiv, partialResponse);
}
});
document.getElementById('clear-chat').addEventListener('click', () => {
window.electronAPI.clearChatHistory();
});
window.electronAPI.onHistoryCleared(() => {
document.getElementById('response-container').innerHTML = '';
// Stop and reset the current audio
stopAudio();
alert('Chat history cleared!');
});
function initializeMediaSource() {
console.log('media source initialized')
mediaSource = new MediaSource();
audioElement.src = URL.createObjectURL(mediaSource);
mediaSource.addEventListener('sourceopen', sourceOpenHandler);
}
function sourceOpenHandler() {
console.log('running sourceopenhandler')
sourceBuffer = mediaSource.addSourceBuffer('audio/mpeg');
sourceBuffer.addEventListener('updateend', () => {
if (audioChunksQueue.length > 0 && !sourceBuffer.updating) {
const nextChunk = audioChunksQueue.shift();
sourceBuffer.appendBuffer(nextChunk);
}
});
}
let audioChunksQueue = [];
window.electronAPI.receive('audio-chunk-received', (chunk) => {
if (sourceBuffer && !sourceBuffer.updating) {
sourceBuffer.appendBuffer(chunk);
playAudio();
} else {
audioChunksQueue.push(chunk);
}
});
function playAudio() {
// Play the audio if it's not already playing
if (audioElement.paused) {
audioElement.play().catch(e => console.error('Error playing audio:', e));
}
}
function stopAudio() {
audioElement.pause();
audioElement.currentTime = 0;
audioElement.src = '';
if (mediaSource.readyState === 'open') {
clearSourceBuffer();
} else {
initializeMediaSource(); // Reinitialize if the MediaSource is not open
}
}
function clearSourceBuffer() {
if (sourceBuffer.updating) {
sourceBuffer.addEventListener('updateend', clearBuffer, { once: true });
} else {
clearBuffer();
}
}
function clearBuffer() {
try {
sourceBuffer.remove(0, audioElement.duration);
} catch (error) {
console.error('Error clearing source buffer:', error);
}
}
document.getElementById('skip-button').addEventListener('click', () => {
stopAudio();
window.electronAPI.send('skip-audio-stream');
initializeMediaSource(); // Reinitialize for the new stream
});
let isMuted = false;
document.getElementById('mute-button').addEventListener('click', () => {
isMuted = !isMuted;
audioElement.muted = isMuted;
document.getElementById('mute-button').textContent = isMuted ? 'Unmute' : 'Mute';
});
let isRecording = false;
let mediaRecorder;
let audioChunks = [];
document.getElementById('toggle-recording').addEventListener('click', async () => {
const recordingButton = document.getElementById('toggle-recording');
if (!isRecording) {
// Start recording
audioChunks = [];
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = (event) => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
window.electronAPI.sendAudioForTranscription(audioBlob);
};
mediaRecorder.start();
recordingButton.textContent = 'Recording: On';
recordingButton.classList.add('active');
isRecording = true;
} else {
// Stop recording
mediaRecorder.stop();
recordingButton.textContent = 'Recording: Off';
recordingButton.classList.remove('active');
isRecording = false;
}
});
window.electronAPI.receive('transcription-complete', (transcribedText) => {
document.getElementById('prompt-input').value = transcribedText;
});
let isCaptureActive = false;
let captureMode = '';
let captureInterval;
document.getElementById('window-select').addEventListener('focus', () => {
window.electronAPI.send('refresh-window-sources');
if (isCaptureActive) {
const selectedWindowId = document.getElementById('window-select').value;
clearInterval(captureInterval); // Stop the current capture interval
captureSelectedWindow(selectedWindowId); // Capture the newly selected window immediately
captureInterval = setInterval(() => captureSelectedWindow(selectedWindowId), 1000); // Restart the capture interval
}
});
window.electronAPI.receive('window-sources-received', (sources) => {
const selectElement = document.getElementById('window-select');
selectElement.innerHTML = ''; // Clear existing options
sources.forEach(source => {
const option = document.createElement('option');
option.value = source.id;
option.innerText = source.name;
selectElement.appendChild(option);
});
});
document.getElementById('capture').addEventListener('click', () => {
isCaptureActive = !isCaptureActive;
const captureButton = document.getElementById('capture');
console.log('Capture state:', isCaptureActive)
const selectedWindowId = document.getElementById('window-select').value;
if (isCaptureActive) {
captureButton.textContent = 'Capture: Active';
captureButton.classList.add('active');
window.electronAPI.requestSourceId(selectedWindowId, 'capture');
captureInterval = setInterval(() => captureSelectedWindow(selectedWindowId), 1000);
} else {
captureButton.textContent = 'Capture: Off';
captureButton.classList.remove('active');
clearInterval(captureInterval);
globalBase64Image = '';
}
});
async function captureSelectedWindow(sourceId) {
try {
const constraints = {
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: sourceId,
minWidth: 1280,
maxWidth: 1280,
minHeight: 720,
maxHeight: 720
}
}
};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
const video = document.createElement('video');
video.srcObject = stream;
video.onloadedmetadata = () => {
video.play();
setTimeout(() => {
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
globalBase64Image = canvas.toDataURL('image/jpeg');
video.srcObject.getTracks().forEach(track => track.stop());
}, 100);
};
} catch (e) {
console.error('Error capturing window:', e);
}
}
window.electronAPI.onReceivedSourceId((sourceId) => {
if (isCaptureActive) {
captureSelectedWindow(sourceId);
}
});
document.getElementById('start-snipping').addEventListener('click', () => {
window.electronAPI.startSnipping();
});
window.electronAPI.receive('snip-initiated', (promptText) => {
promptInput.value = '';
appendUserPrompt(promptText); // Display the actual prompt used for the snip
currentResponseDiv = createResponseDiv();
});
document.getElementById('settings-button').addEventListener('click', () => {
document.getElementById('settings-modal').style.display = 'block';
});
document.querySelector('.close-button').addEventListener('click', () => {
document.getElementById('settings-modal').style.display = 'none';
});
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
let modal = document.getElementById('settings-modal');
if (event.target === modal) {
modal.style.display = 'none';
}
}
// Save settings button functionality
document.getElementById('save-settings').addEventListener('click', () => {
// You can add logic here to save the API key and selected window
// For now, we'll just close the modal
document.getElementById('settings-modal').style.display = 'none';
});