-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
179 lines (154 loc) · 5.38 KB
/
main.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
require('dotenv').config();
const { app, BrowserWindow, screen, ipcMain, nativeTheme, desktopCapturer } = require('electron');
const openaiService = require('./services/openaiService');
const path = require('node:path');
let snippingWindow = null;
let mainWindow = null;
let currentPromptText = '';
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
}
});
//mainWindow.setMenu(null);
mainWindow.loadFile('index.html');
mainWindow.webContents.on('did-finish-load', () => {
fetchWindowSources(); // Fetch window sources after the main window is ready
});
}
function createSnippingWindow() {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
snippingWindow = new BrowserWindow({
width,
height,
frame: false,
transparent: true,
alwaysOnTop: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
preload: path.join(__dirname, 'preload.js')
}
});
snippingWindow.loadFile('snip.html');
snippingWindow.setFullScreen(true);
snippingWindow.setSkipTaskbar(true);
}
ipcMain.handle('dark-mode:toggle', () => {
nativeTheme.themeSource = nativeTheme.shouldUseDarkColors ? 'light' : 'dark';
return nativeTheme.shouldUseDarkColors;
});
ipcMain.handle('dark-mode:system', () => {
nativeTheme.themeSource = 'system';
});
ipcMain.on('update-prompt-text', (event, promptText) => {
currentPromptText = promptText;
});
ipcMain.on('send-prompt', async (event, { prompt, base64Image }) => {
try {
//let fullTextResponse = ''; // To accumulate the full text response
// Call the OpenAI service with a callback for streamed text
await openaiService.callOpenAI(prompt, base64Image, mainWindow);
currentPromptText = ''
// Once the full text response is received, handle text-to-speech
// if (fullTextResponse) {
// const audioFilePath = await openaiService.textToSpeech(fullTextResponse);
// event.sender.send('response-received', { text: fullTextResponse, audioFilePath });
// }
} catch (error) {
console.error('Error handling send-prompt:', error);
}
});
ipcMain.on('clear-history', () => {
mainWindow.webContents.send('history-cleared');
openaiService.clearMessageHistory();
openaiService.stopCurrentStream(); // Make sure to stop the TTS stream
});
ipcMain.on('transcribe-audio', async (event, audioBuffer) => {
try {
const transcribedText = await openaiService.transcribeAudio(audioBuffer);
currentPromptText = transcribedText
event.sender.send('transcription-complete', transcribedText);
} catch (error) {
console.error('Error transcribing audio:', error);
}
});
ipcMain.on('skip-audio-stream', () => {
openaiService.stopCurrentStream();
// You can also add additional logic here if needed
});
ipcMain.on('send-snip', async (event, base64Image) => {
//console.log('Base64 Image received:', base64Image);
// Here you can process the base64 image, e.g., send it to OpenAI Vision API
});
ipcMain.on('start-snipping', (event) => {
createSnippingWindow();
});
ipcMain.on('snip-complete', async (event, base64Image) => {
if (snippingWindow) {
snippingWindow.close();
snippingWindow = null;
}
// Send the current prompt along with the notification
mainWindow.webContents.send('snip-initiated', currentPromptText);
try {
await openaiService.callOpenAI(currentPromptText, base64Image, mainWindow);
} catch (error) {
console.error('Error calling OpenAI with snip:', error);
}
});
ipcMain.on('REQUEST_SOURCE_ID', async (event, { sourceId, mode }) => {
try {
const sources = await desktopCapturer.getSources({ types: ['screen', 'window'] });
if (mode === 'capture') {
// Handle window capture logic
const selectedSource = sources.find(source => source.id === sourceId);
event.reply('RECEIVED_SOURCE_ID', selectedSource.id);
} else if (mode === 'snip') {
// Handle screen capture logic for snipping
const screenSource = sources.find(source => source.id.includes('screen'));
event.reply('RECEIVED_SOURCE_ID', screenSource.id);
}
} catch (error) {
console.error('Error in REQUEST_SOURCE_ID:', error);
}
});
async function fetchWindowSources() {
try {
const sources = await desktopCapturer.getSources({ types: ['window'] });
// Process and send these sources to the renderer process
// You might want to send the source names and their respective IDs
mainWindow.webContents.send('window-sources-received', sources.map(source => {
return { id: source.id, name: source.name };
}));
} catch (error) {
console.error('Error fetching window sources:', error);
// Handle the error appropriately
}
}
ipcMain.on('refresh-window-sources', async (event) => {
try {
const sources = await desktopCapturer.getSources({ types: ['window'] });
event.sender.send('window-sources-received', sources.map(source => {
return { id: source.id, name: source.name };
}));
} catch (error) {
console.error('Error refreshing window sources:', error);
}
});
app.whenReady().then(createWindow);
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});