UNPKG

nwhisper

Version:

Native Node.js bindings for OpenAI's Whisper using whisper.cpp. High-performance local speech-to-text with custom model support.

130 lines 5.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.whisperShell = whisperShell; exports.executeCppCommand = executeCppCommand; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const shelljs_1 = __importDefault(require("shelljs")); const constants_1 = require("./constants"); const projectDir = process.cwd(); const defaultShellOptions = { silent: false, async: true, }; function handleError(error, logger = console) { logger.error('[nwhisper] Error:', error.message); shelljs_1.default.cd(projectDir); throw error; } // Get the correct executable path based on platform and build system function getWhisperExecutablePath() { const execName = process.platform === 'win32' ? 'whisper-cli.exe' : 'whisper-cli'; // Check common CMake build locations const possiblePaths = [ node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', 'bin', execName), // Unix CMake node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', 'bin', 'Release', execName), // Windows CMake Release node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', 'bin', 'Debug', execName), // Windows CMake Debug node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', execName), // Alternative location node_path_1.default.join(constants_1.WHISPER_CPP_PATH, execName), // Root directory ]; for (const execPath of possiblePaths) { if (node_fs_1.default.existsSync(execPath)) { return execPath; } } return ''; // Not found } // Check if whisper-cli executable exists function checkExecutableExists(logger = console) { const execPath = getWhisperExecutablePath(); const exists = execPath !== ''; if (exists) { logger.debug(`[nwhisper] Found executable at: ${execPath}`); } else { logger.debug('[nwhisper] Executable not found in any expected location'); } return exists; } // Check if build directory exists and has been configured function isBuildConfigured() { const buildDir = node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build'); const cmakeCache = node_path_1.default.join(buildDir, 'CMakeCache.txt'); return node_fs_1.default.existsSync(buildDir) && node_fs_1.default.existsSync(cmakeCache); } async function whisperShell(command, options = defaultShellOptions, logger = console) { return new Promise((resolve, reject) => { const shellOptions = { ...options, windowsHide: true, // Prevent command window popup on Windows }; shelljs_1.default.exec(command, shellOptions, (code, stdout, stderr) => { logger.debug('Exit code:', code); logger.debug('Stdout:', stdout); logger.debug('Stderr:', stderr); if (code === 0) { if (stdout.includes('error:')) { reject(new Error(`Error in whisper.cpp:\n${stdout}`)); return; } logger.debug('[nwhisper] Transcribing Done!'); resolve(stdout); } else { reject(new Error(stderr || `Command failed with exit code ${code}`)); } }); }).catch((error) => { handleError(error, logger); return Promise.reject(error); }); } async function executeCppCommand(command, logger = console, withCuda = false) { try { shelljs_1.default.cd(constants_1.WHISPER_CPP_PATH); // Check if executable already exists if (!checkExecutableExists(logger)) { logger.debug('[nwhisper] whisper-cli executable not found. Building...'); // Configure build if not already configured if (!isBuildConfigured()) { logger.debug('[nwhisper] Configuring CMake build...'); let configureCommand = 'cmake -B build'; if (withCuda) { configureCommand += ' -DGGML_CUDA=1'; } const configResult = shelljs_1.default.exec(configureCommand); if (configResult.code !== 0) { throw new Error(`[nwhisper] CMake configuration failed: ${configResult.stderr}`); } logger.debug('[nwhisper] CMake configuration completed.'); } else { logger.debug('[nwhisper] Build already configured.'); } // Build the project logger.debug('[nwhisper] Building whisper.cpp...'); const buildCommand = 'cmake --build build --config Release'; const buildResult = shelljs_1.default.exec(buildCommand); if (buildResult.code !== 0) { throw new Error(`[nwhisper] Build failed: ${buildResult.stderr}`); } // Verify executable was created if (!checkExecutableExists(logger)) { throw new Error('[nwhisper] Build completed but executable not found. Please check the build output for errors.'); } logger.log('[nwhisper] Build completed successfully.'); } else { logger.debug('[nwhisper] whisper-cli executable found. Skipping build.'); } return await whisperShell(command, defaultShellOptions, logger); } catch (error) { handleError(error, logger); throw error; } } //# sourceMappingURL=whisper.js.map