Repository navigation
Expand file tree
/
Copy pathwhisper-manager.js
More file actions
368 lines (334 loc) · 13.4 KB
/
Copy pathwhisper-manager.js
File metadata and controls
368 lines (334 loc) · 13.4 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
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
// whisper-manager.js — owns the local whisper.cpp HTTP server.
//
// Two run modes, switched by user via Settings:
// 1. "child" (default) — Switchboard spawns whisper-server.exe as a child
// process at app start, kills it on app close. Health-checks before
// reporting ready. No admin needed.
// 2. "scheduled" — User clicked "Install on startup". A Windows logon-
// triggered scheduled task launches whisper-server in the user's
// session at every login. Switchboard detects the running server
// and skips its own spawn.
//
// We deliberately use Scheduled Tasks (schtasks.exe), not Windows
// Services / NSSM. Reasons: services run as SYSTEM by default which
// breaks user-session audio device assumptions, and registering a
// service triggers UAC. A logon scheduled task runs in the user's own
// session, doesn't need admin, and can be removed cleanly with one
// schtasks /Delete call.
const fs = require('fs');
const path = require('path');
const os = require('os');
const http = require('http');
const { spawn } = require('child_process');
const DEFAULT_PORT = 52391;
const DEFAULT_HOST = '127.0.0.1';
const HEALTH_TIMEOUT_MS = 30000;
const RESTART_BACKOFF_MS = [1000, 2000, 4000, 8000, 16000];
const MAX_LOG_LINES = 200;
// Detected whisper.cpp install paths to try in order. The first one with a
// matching whisper-server.exe is used unless the user overrides via settings.
// Most-permanent locations first so users who relocate from a build dir to
// a managed install path automatically pick up the move on next launch.
const WHISPER_BINARY_CANDIDATES = [
// Recommended permanent install locations (managed, won't disappear when
// the user cleans up build outputs).
'C:/whisper-cpp/whisper-server.exe',
'C:/Program Files/whisper-cpp/whisper-server.exe',
// Build-tree fallbacks for users running directly from a clone.
'D:/development/whisper.cpp-src/build/bin/Release/whisper-server.exe',
'C:/development/whisper.cpp-src/build/bin/Release/whisper-server.exe',
];
const MODEL_FILENAME = 'ggml-large-v3-turbo.bin';
// Model search order, most-specific first:
// 1. Explicit settings override (Settings → Voice → Model Path)
// 2. Same directory as whisper-server.exe (most permissive — works no
// matter where the user dropped the file)
// 3. <repo-root>/models/ for the standard whisper.cpp project layout
// (binary at <root>/build/bin/Release/, models at <root>/models/)
// 4. <repo-root>/build/models/ — alternative layouts seen in the wild
// 5. <userData>/whisper-models/ — Switchboard-managed fallback
// We don't auto-download in v1; we surface a clear error and the user
// fetches it via the existing whisper.cpp download-ggml-model script.
function modelSearchPaths(opts, userDataDir) {
const out = [];
if (opts.modelPath) out.push(opts.modelPath);
if (opts.binaryPath) {
const binDir = path.dirname(opts.binaryPath);
// 2: alongside the binary
out.push(path.join(binDir, MODEL_FILENAME));
// 3: <root>/models/ — for whisper.cpp's canonical layout, that's
// 4 dirnames up from <root>/build/bin/Release/<exe>.
const fourUp = path.dirname(path.dirname(path.dirname(path.dirname(opts.binaryPath))));
out.push(path.join(fourUp, 'models', MODEL_FILENAME));
// 4: <build>/models/ — 3 dirnames up.
const threeUp = path.dirname(path.dirname(path.dirname(opts.binaryPath)));
out.push(path.join(threeUp, 'models', MODEL_FILENAME));
}
if (userDataDir) out.push(path.join(userDataDir, 'whisper-models', MODEL_FILENAME));
return out;
}
let _state = {
status: 'stopped', // 'stopped' | 'starting' | 'ready' | 'error'
error: null,
port: DEFAULT_PORT,
host: DEFAULT_HOST,
binaryPath: null,
modelPath: null,
managedBy: 'none', // 'child' | 'external' | 'none'
logTail: [], // last MAX_LOG_LINES from stdout/stderr
pid: null,
};
let _child = null;
let _restartCount = 0;
let _stopping = false;
let _logger = null;
let _settings = {};
let _userDataDir = null;
let _onStateChange = null;
let _diskLogStream = null; // append-only fs stream for whisper-server stdio
function openDiskLog() {
if (_diskLogStream || !_userDataDir) return;
try {
const dir = path.join(_userDataDir, 'logs');
fs.mkdirSync(dir, { recursive: true });
_diskLogStream = fs.createWriteStream(path.join(dir, 'whisper.log'), { flags: 'a' });
_diskLogStream.write(`\n=== whisper-server session ${new Date().toISOString()} ===\n`);
} catch (err) {
if (_logger) _logger.warn('[whisper] failed to open disk log:', err.message);
}
}
function writeDiskLog(line) {
if (!_diskLogStream) return;
try { _diskLogStream.write(line + '\n'); } catch {}
}
function getStatus() {
return {
status: _state.status,
error: _state.error,
port: _state.port,
host: _state.host,
binaryPath: _state.binaryPath,
modelPath: _state.modelPath,
managedBy: _state.managedBy,
pid: _state.pid,
logTail: _state.logTail.slice(-50),
endpoint: `http://${_state.host}:${_state.port}/inference`,
};
}
function setState(patch) {
_state = { ..._state, ...patch };
if (_onStateChange) {
try { _onStateChange(getStatus()); } catch {}
}
}
function pushLog(line) {
if (!line) return;
_state.logTail.push(line);
if (_state.logTail.length > MAX_LOG_LINES) _state.logTail.shift();
}
function findBinary(opts) {
if (opts && opts.binaryPath && fs.existsSync(opts.binaryPath)) return opts.binaryPath;
for (const p of WHISPER_BINARY_CANDIDATES) {
if (fs.existsSync(p)) return p;
}
return null;
}
function findModel(opts, userDataDir) {
for (const p of modelSearchPaths(opts || {}, userDataDir)) {
if (p && fs.existsSync(p)) return p;
}
return null;
}
// HTTP probe — assumes the server is alive when GET / returns any 2xx/4xx
// (whisper-server returns its index page; even a 404 is fine — means TCP +
// HTTP layer are responsive).
function probe(host, port, timeoutMs) {
return new Promise((resolve) => {
const req = http.get({ host, port, path: '/', timeout: timeoutMs || 1500 }, (res) => {
res.resume();
resolve(true);
});
req.on('error', () => resolve(false));
req.on('timeout', () => { req.destroy(); resolve(false); });
});
}
async function waitUntilReady(host, port, deadlineMs) {
const deadline = Date.now() + deadlineMs;
while (Date.now() < deadline) {
if (await probe(host, port, 1500)) return true;
await new Promise(r => setTimeout(r, 400));
}
return false;
}
async function detectExternal(host, port) {
// If something already responds on the configured port, treat it as
// externally-managed (e.g. user installed the scheduled task, or runs
// whisper-server themselves) and don't spawn our own.
return probe(host, port, 1000);
}
async function spawnChild() {
if (_stopping) return;
const binary = findBinary(_settings);
if (!binary) {
setState({
status: 'error',
error: 'whisper-server.exe not found. Set its path in Settings → Voice or build whisper.cpp.',
managedBy: 'none',
});
return;
}
// Pass the *discovered* binary path into the model search so binary-
// relative locations (binary-adjacent dir, project-root models/, etc.)
// are checked even when the user hasn't pinned a binaryPath in settings.
const model = findModel({ ..._settings, binaryPath: binary }, _userDataDir);
if (!model) {
setState({
status: 'error',
error: `Model "${MODEL_FILENAME}" not found. Drop it next to whisper-server, or place it under "${path.join(_userDataDir || '', 'whisper-models')}".`,
binaryPath: binary,
managedBy: 'none',
});
return;
}
setState({
status: 'starting',
error: null,
binaryPath: binary,
modelPath: model,
managedBy: 'child',
pid: null,
});
const args = [
'-m', model,
'--host', _state.host,
'--port', String(_state.port),
// -1 lets ggml pick CPU thread count; default 4 is usually fine on modern boxes.
'--threads', String(Math.max(4, Math.min(16, os.cpus().length || 4))),
// Accept any audio format the renderer sends — whisper.cpp's server
// only natively reads WAV, but with --convert it shells out to ffmpeg
// to transcode (Opus/WebM/m4a/etc → WAV) before transcribing. Lets
// us record with MediaRecorder (browser-native Opus codec) and
// submit the file directly without resampling/decoding in renderer.
'--convert',
];
if (_settings.extraArgs && Array.isArray(_settings.extraArgs)) args.push(..._settings.extraArgs);
const child = spawn(binary, args, {
cwd: path.dirname(binary),
windowsHide: true,
stdio: ['ignore', 'pipe', 'pipe'],
});
_child = child;
setState({ pid: child.pid });
openDiskLog();
child.stdout.on('data', (d) => {
for (const line of d.toString('utf8').split('\n')) {
const s = line.trimEnd();
if (s) { pushLog(s); writeDiskLog(`[out] ${s}`); }
}
});
child.stderr.on('data', (d) => {
for (const line of d.toString('utf8').split('\n')) {
const s = line.trimEnd();
if (s) { pushLog(s); writeDiskLog(`[err] ${s}`); }
}
});
child.on('exit', (code, signal) => {
if (_logger) _logger.info(`[whisper] child exited code=${code} signal=${signal}`);
writeDiskLog(`[lifecycle] child exited code=${code} signal=${signal}`);
_child = null;
if (_stopping) return;
// Unexpected exit → backoff restart.
const delay = RESTART_BACKOFF_MS[Math.min(_restartCount, RESTART_BACKOFF_MS.length - 1)];
_restartCount++;
setState({ status: 'error', error: `whisper-server exited (${code}); restarting in ${delay}ms`, pid: null });
setTimeout(() => { if (!_stopping) spawnChild(); }, delay);
});
child.on('error', (err) => {
if (_logger) _logger.error('[whisper] spawn error:', err.message);
setState({ status: 'error', error: err.message, pid: null });
});
const ready = await waitUntilReady(_state.host, _state.port, HEALTH_TIMEOUT_MS);
if (ready) {
_restartCount = 0;
setState({ status: 'ready', error: null });
if (_logger) _logger.info(`[whisper] ready at http://${_state.host}:${_state.port}`);
} else {
setState({ status: 'error', error: 'whisper-server failed to respond within timeout' });
}
}
function killChild() {
_stopping = true;
if (_child) {
try { _child.kill(); } catch {}
_child = null;
}
setState({ status: 'stopped', pid: null, managedBy: 'none' });
}
async function start() {
_stopping = false;
setState({ port: _settings.port || DEFAULT_PORT, host: _settings.host || DEFAULT_HOST });
// Externally managed (scheduled task or user-launched)?
if (await detectExternal(_state.host, _state.port)) {
const detectedBinary = findBinary(_settings);
setState({
status: 'ready',
error: null,
managedBy: 'external',
binaryPath: detectedBinary,
modelPath: findModel({ ..._settings, binaryPath: detectedBinary }, _userDataDir),
});
if (_logger) _logger.info(`[whisper] using externally-managed server at ${_state.host}:${_state.port}`);
return;
}
if (_settings.autoStart === false) {
setState({ status: 'stopped', managedBy: 'none' });
return;
}
await spawnChild();
}
// Note: scheduled-task install/uninstall is intentionally NOT performed
// from inside Switchboard. It's been left to the user's own schtasks.exe
// or Task Scheduler GUI invocation — the in-app paths added more friction
// (UAC quirks, restricted-token failures) than they removed. Switchboard
// still detects an externally-managed whisper-server on the configured
// port via probe(), so a user-installed scheduled task plugs in cleanly.
// ── Lifecycle ────────────────────────────────────────────────────────────
async function init({ log, app, getMainWindow, ipcMain, getVoiceSettings }) {
_logger = log;
_userDataDir = app.getPath('userData');
// Settings come from the caller — main.js owns the settings store and
// passes a getter so this module stays decoupled.
_settings = (typeof getVoiceSettings === 'function' ? getVoiceSettings() : null) || {};
ipcMain.handle('whisper:status', () => getStatus());
// Quick ping — useful for the settings panel "Test connection" button.
// Returns whether the configured host:port responds to HTTP.
ipcMain.handle('whisper:ping', async () => {
const ok = await probe(_state.host, _state.port, 1500);
return { ok, host: _state.host, port: _state.port, endpoint: `http://${_state.host}:${_state.port}/inference` };
});
ipcMain.handle('whisper:start', async () => { await start(); return getStatus(); });
ipcMain.handle('whisper:stop', () => { killChild(); return getStatus(); });
ipcMain.handle('whisper:restart', async () => { killChild(); _stopping = false; await start(); return getStatus(); });
ipcMain.handle('whisper:update-settings', async (_e, next) => {
_settings = { ..._settings, ...(next || {}) };
return { ok: true };
});
_onStateChange = (s) => {
const win = getMainWindow && getMainWindow();
if (win && !win.isDestroyed()) {
try { win.webContents.send('whisper-state', s); } catch {}
}
};
app.on('before-quit', killChild);
// Don't block startup — kick start in the background.
start().catch((err) => log && log.error('[whisper] start failed:', err.message));
}
module.exports = {
init,
start,
killChild,
getStatus,
// Exposed for tests
_internal: { findBinary, findModel, modelSearchPaths, probe, waitUntilReady },
DEFAULT_PORT, DEFAULT_HOST,
};