-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
312 lines (262 loc) · 9.31 KB
/
Copy pathmain.js
File metadata and controls
312 lines (262 loc) · 9.31 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
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
const { app, BrowserWindow, globalShortcut, ipcMain, clipboard, Tray, Menu, systemPreferences, shell, nativeImage, screen } = require('electron');
const path = require('path');
const fs = require('fs');
const OpenAI = require('openai');
const { exec } = require('child_process');
const Store = require('electron-store');
require('dotenv').config();
const store = new Store();
let mainWindow;
let indicatorWindows = [];
let tray;
let isRecording = false;
let openai;
// Default config
let currentConfig = {
key: store.get('apiKey') || process.env.OPENAI_API_KEY,
provider: store.get('provider') || 'openai',
shortcut: store.get('shortcut') || 'Option+Space',
showIndicator: store.get('showIndicator') !== false // Default true
};
function configureOpenAI() {
if (!currentConfig.key) return;
const config = { apiKey: currentConfig.key };
if (currentConfig.provider === 'groq') {
config.baseURL = 'https://api.groq.com/openai/v1';
}
openai = new OpenAI(config);
}
function createMainWindow() {
mainWindow = new BrowserWindow({
width: 500,
height: 600,
show: false, // Ensure hidden
titleBarStyle: 'hiddenInset', // macOS style
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
},
});
mainWindow.loadFile('index.html');
// Send initial stats
mainWindow.webContents.on('did-finish-load', () => {
const stats = {
totalWords: store.get('stats.totalWords') || 0
};
mainWindow.webContents.send('update-stats', stats);
mainWindow.webContents.send('load-config', currentConfig);
});
mainWindow.on('close', (event) => {
if (!app.isQuitting) {
event.preventDefault();
mainWindow.hide();
}
return false;
});
}
function getIconPath(iconName) {
const localPath = path.join(__dirname, iconName);
if (fs.existsSync(localPath)) return localPath;
const resourcePath = path.join(process.resourcesPath, iconName);
if (fs.existsSync(resourcePath)) return resourcePath;
return localPath; // Fallback
}
function createIndicatorWindows() {
// Close existing windows
indicatorWindows.forEach(w => {
if (!w.isDestroyed()) w.close();
});
indicatorWindows = [];
if (!currentConfig.showIndicator) return;
const displays = screen.getAllDisplays();
displays.forEach(display => {
const win = new BrowserWindow({
width: 200,
height: 60,
x: display.bounds.x + Math.round(display.bounds.width / 2 - 100),
y: display.bounds.y + display.bounds.height - 80,
frame: false,
transparent: true,
alwaysOnTop: true,
focusable: false,
skipTaskbar: true,
hasShadow: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
win.loadFile('indicator.html');
win.setIgnoreMouseEvents(true);
win.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
// Initial status
const status = isRecording ? 'recording' : 'idle';
win.webContents.on('did-finish-load', () => {
win.webContents.send('update-indicator-status', status);
});
indicatorWindows.push(win);
});
}
function updateIndicatorStatus(status) {
indicatorWindows.forEach(w => {
if (!w.isDestroyed()) {
w.webContents.send('update-indicator-status', status);
}
});
}
function updateTrayIcon() {
if (!tray) return;
const iconName = isRecording ? 'iconRecordingTemplate.svg' : 'iconTemplate.svg';
const iconPath = getIconPath(iconName);
const icon = nativeImage.createFromPath(iconPath);
icon.setTemplateImage(true);
// Resize to 16x16 or 22x22 for menu bar
tray.setImage(icon);
// Update Indicator Windows
const status = isRecording ? 'recording' : 'idle';
updateIndicatorStatus(status);
// Update Tray Menu Status
updateTrayMenu();
}
function updateTrayMenu() {
if (!tray) return;
const statusText = isRecording ? 'Recording...' : 'Ready';
const contextMenu = Menu.buildFromTemplate([
{ label: `Status: ${statusText}`, enabled: false },
{ type: 'separator' },
{ label: 'Settings...', click: () => mainWindow.show() },
{ type: 'separator' },
{
label: 'Quit', click: () => {
app.isQuitting = true;
app.quit();
}
}
]);
tray.setContextMenu(contextMenu);
}
function createTray() {
const iconPath = getIconPath('iconTemplate.svg');
const icon = nativeImage.createFromPath(iconPath);
icon.setTemplateImage(true);
tray = new Tray(icon);
updateTrayMenu();
tray.setToolTip('AlphaVoice');
}
function registerShortcut() {
globalShortcut.unregisterAll();
try {
const ret = globalShortcut.register(currentConfig.shortcut, () => {
console.log('Shortcut pressed');
if (isRecording) {
mainWindow.webContents.send('stop-recording');
isRecording = false;
updateTrayIcon();
} else {
mainWindow.webContents.send('start-recording');
isRecording = true;
updateTrayIcon();
}
});
if (!ret) {
console.log('Registration failed for', currentConfig.shortcut);
return false;
}
return true;
} catch (err) {
console.error('Shortcut registration error', err);
return false;
}
}
async function handleRecordingFinished(event, buffer) {
console.log('Processing audio...');
console.log('Processing audio...');
mainWindow.webContents.send('status-update', 'Transcribing...');
updateIndicatorStatus('processing');
try {
if (!currentConfig.key) {
mainWindow.show();
mainWindow.webContents.send('error', 'Please set your API Key');
isRecording = false;
updateTrayIcon();
return;
}
if (!openai) configureOpenAI();
const tempFilePath = path.join(app.getPath('userData'), 'temp_recording.webm');
fs.writeFileSync(tempFilePath, Buffer.from(buffer));
const model = currentConfig.provider === 'groq' ? 'whisper-large-v3' : 'whisper-1';
const transcription = await openai.audio.transcriptions.create({
file: fs.createReadStream(tempFilePath),
model: model,
});
const text = transcription.text;
console.log('Transcription:', text);
if (text) {
// Update stats
const wordCount = text.trim().split(/\s+/).length;
const totalWords = (store.get('stats.totalWords') || 0) + wordCount;
store.set('stats.totalWords', totalWords);
mainWindow.webContents.send('update-stats', { totalWords });
// Paste text
clipboard.writeText(text);
pasteText();
mainWindow.webContents.send('status-update', 'Ready');
updateIndicatorStatus('idle');
}
} catch (error) {
console.error('Error:', error);
mainWindow.webContents.send('error', error.message);
} finally {
isRecording = false;
updateTrayIcon();
}
}
function pasteText() {
const script = `tell application "System Events" to keystroke "v" using command down`;
exec(`osascript -e '${script}'`, (error) => {
if (error) console.error('Paste failed:', error);
});
}
app.whenReady().then(async () => {
if (process.platform === 'darwin') {
app.dock.hide(); // Hide dock icon
await systemPreferences.askForMediaAccess('microphone');
}
createMainWindow();
createTray();
createIndicatorWindows();
registerShortcut();
screen.on('display-added', createIndicatorWindows);
screen.on('display-removed', createIndicatorWindows);
screen.on('display-metrics-changed', createIndicatorWindows);
ipcMain.on('recording-finished', handleRecordingFinished);
ipcMain.on('save-config', (event, config) => {
currentConfig = { ...currentConfig, ...config };
store.set('apiKey', config.key);
store.set('provider', config.provider);
store.set('shortcut', config.shortcut);
store.set('showIndicator', config.showIndicator);
createIndicatorWindows();
configureOpenAI();
const success = registerShortcut();
if (success) {
mainWindow.webContents.send('shortcut-registered', currentConfig.shortcut);
} else {
mainWindow.webContents.send('error', `Failed to register shortcut: ${currentConfig.shortcut}`);
}
console.log('Config updated');
});
app.on('activate', () => {
// On dock click (if dock icon was visible), show settings.
// Since we hide dock, this might not be triggered often, but good to have.
if (BrowserWindow.getAllWindows().length === 0) createMainWindow();
else if (mainWindow) mainWindow.show();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
app.on('will-quit', () => {
globalShortcut.unregisterAll();
});