Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): ship sherpa-onnx-node in the packaged app

Elian Doran74d94a53ea6b
diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml
index 8330fbd510..46f518adac 100644
--- a/.github/workflows/nightly.yml
+++ b/.github/workflows/nightly.yml
@@ -97,7 +97,9 @@ jobs:
with:
node-version: 24
- name: Install dependencies
- run: pnpm install --frozen-lockfile
+ # The arm64 macOS runner also packages darwin-x64, which needs that architecture's native
+ # optional dependencies (sherpa-onnx-darwin-x64) beside the host's.
+ run: pnpm install --frozen-lockfile ${{ matrix.os.name == 'macos' && '--cpu=x64,arm64' || '' }}
- name: Update nightly version
run: pnpm run chore:ci-update-nightly-version
- name: Run the build
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index c37646ed7b..c6d1cfb581 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -83,7 +83,9 @@ jobs:
node-version: 24
cache: 'pnpm'
- name: Install dependencies
- run: pnpm install --frozen-lockfile
+ # The arm64 macOS runner also packages darwin-x64, which needs that architecture's native
+ # optional dependencies (sherpa-onnx-darwin-x64) beside the host's.
+ run: pnpm install --frozen-lockfile ${{ matrix.os.name == 'macos' && '--cpu=x64,arm64' || '' }}
- name: Run the build
uses: ./.github/actions/build-electron
with:
diff --git a/apps/desktop/electron-forge/forge.config.ts b/apps/desktop/electron-forge/forge.config.ts
index b5118dd92a..c819b55848 100644
--- a/apps/desktop/electron-forge/forge.config.ts
+++ b/apps/desktop/electron-forge/forge.config.ts
@@ -43,7 +43,11 @@ const config: ForgeConfig = {
name: PRODUCT_NAME,
appVersion: packageJson.version,
overwrite: true,
- asar: true,
+ asar: {
+ // The shared libraries beside sherpa-onnx's addon (onnxruntime) are loaded by the OS,
+ // which cannot read them from inside the archive. auto-unpack-natives adds `*.node`.
+ unpack: "**/node_modules/sherpa-onnx-*-*/**"
+ },
icon: path.join(APP_ICON_PATH, isNightly ? "icon-dev" : "icon"),
...macosSignConfiguration,
windowsSign: windowsSignConfiguration,
diff --git a/apps/desktop/scripts/build.ts b/apps/desktop/scripts/build.ts
index a03ed0dfdb..af9d0e8501 100644
--- a/apps/desktop/scripts/build.ts
+++ b/apps/desktop/scripts/build.ts
@@ -1,7 +1,7 @@
-import { writeFileSync } from "fs";
+import { existsSync, readFileSync, writeFileSync } from "fs";
import { join } from "path";
-import BuildHelper from "../../../scripts/build-utils";
+import BuildHelper, { targetArch } from "../../../scripts/build-utils";
import originalPackageJson from "../package.json" with { type: "json" };
const build = new BuildHelper("apps/desktop");
@@ -41,6 +41,7 @@ async function main() {
build.copyNodeModules([ "better-sqlite3" ]);
// No musl build: Electron itself ships glibc-only Linux binaries.
build.trimBetterSqlite3({ includeMusl: false });
+ copySherpaOnnx();
build.copy("/node_modules/ckeditor5/dist/ckeditor5-content.css", "ckeditor5-content.css");
build.copy("/packages/ckeditor5/src/theme/content_colors.css", "ckeditor5-content-colors.css");
@@ -51,6 +52,29 @@ async function main() {
generatePackageJson();
}
+/**
+ * Copies `sherpa-onnx-node`, which the meeting transcription worker loads, together with the
+ * package holding its native addon for the target platform. That package is only an optional
+ * dependency, so pnpm installs it just for the host's platform; CI installs the others it
+ * packages for with `--cpu`. A target with no such package (Windows on arm64) ships neither.
+ */
+function copySherpaOnnx() {
+ const sherpaDir = join(build.rootDir, "node_modules", "sherpa-onnx-node");
+ const sherpaPackageJson = readFileSync(join(sherpaDir, "package.json"), "utf-8");
+ const { optionalDependencies } = JSON.parse(sherpaPackageJson);
+ const platform = process.platform === "win32" ? "win" : process.platform;
+ const platformPackage = `sherpa-onnx-${platform}-${targetArch()}`;
+ if (!(platformPackage in optionalDependencies)) {
+ console.warn(`sherpa-onnx has no ${platformPackage}; transcription is unavailable there.`);
+ return;
+ }
+ if (!existsSync(join(build.rootDir, "node_modules", platformPackage))) {
+ throw new Error(`${platformPackage} is not installed. Install the target architecture's `
+ + `optional dependencies too: pnpm install --cpu=${process.arch},${targetArch()}`);
+ }
+ build.copyNodeModules([ "sherpa-onnx-node", platformPackage ]);
+}
+
function generatePackageJson() {
const { version, author, license, description, dependencies, devDependencies } = originalPackageJson;
const packageJson = {
diff --git a/scripts/build-utils.ts b/scripts/build-utils.ts
index a35ddbb4ce..dfe08178c0 100644
--- a/scripts/build-utils.ts
+++ b/scripts/build-utils.ts
@@ -277,7 +277,8 @@ export default class BuildHelper {
* The architecture currently being built for. The release workflows set these when
* a job's target differs from the runner it happens to be executing on.
*/
-function targetArch() {
+/** The architecture being built for, which CI can set apart from the host's. */
+export function targetArch() {
return process.env.TARGET_ARCH || process.env.MATRIX_ARCH || process.arch;
}