const fs = require("fs"); const path = require("path"); const ROOT = path.resolve(__dirname, ".."); const DATA = path.join(ROOT, "data"); function ensureDataDirs() { for (const name of ["logs", "models", "runtime", "tmp"]) fs.mkdirSync(path.join(DATA, name), { recursive: true }); } function dataPath(...parts) { const target = path.resolve(DATA, ...parts); if (target !== DATA && !target.startsWith(`${DATA}${path.sep}`)) throw new Error("Path escapes transcription plugin data."); return target; } function activeWorkerExecutable() { try { const requested = fs.readFileSync(dataPath("runtime", "active-worker.txt"), "utf8").trim(); const target = path.resolve(requested); const runtimeRoot = dataPath("runtime"); return target.startsWith(`${runtimeRoot}${path.sep}`) && fs.statSync(target).isFile() ? target : ""; } catch { return ""; } } function setActiveWorkerExecutable(target) { const executable = path.resolve(String(target || "")); const runtimeRoot = dataPath("runtime"); if (!executable.startsWith(`${runtimeRoot}${path.sep}`) || !fs.existsSync(executable) || !fs.statSync(executable).isFile()) throw new Error("Active transcription worker must be an installed runtime file."); fs.writeFileSync(dataPath("runtime", "active-worker.txt"), `${executable}\n`, { encoding: "utf8", mode: 0o600 }); } function resolveWorkerExecutable(override = "") { const requested = String(override || "").trim(); if (requested) return fs.existsSync(requested) ? path.resolve(requested) : requested; const executable = process.platform === "win32" ? "lumi-whisper-worker.exe" : "lumi-whisper-worker"; const candidates = [ activeWorkerExecutable(), dataPath("runtime", process.platform === "win32" ? "windows-x64-cuda-12.4" : `${process.platform}-${process.arch}-cuda`, "bin", executable), dataPath("runtime", "worker", process.platform === "win32" ? "windows-x64-cuda" : `${process.platform}-${process.arch}-cuda`, "bin", executable), dataPath("runtime", process.platform === "win32" ? "windows-x64-cpu" : `${process.platform}-${process.arch}`, "bin", executable), dataPath("runtime", "worker", process.platform === "win32" ? "windows-x64-cpu" : `${process.platform}-${process.arch}`, "bin", executable), path.join(ROOT, "backend", "transcription", "worker-native", "build", executable) ]; return candidates.find((candidate) => fs.existsSync(candidate) && fs.statSync(candidate).isFile()) || ""; } module.exports = { ROOT, DATA, ensureDataDirs, dataPath, resolveWorkerExecutable, setActiveWorkerExecutable };