diff --git a/packages/cli/src/utils/optionalPackages.ts b/packages/cli/src/utils/optionalPackages.ts index d24b98ccb4..b3acd70eed 100644 --- a/packages/cli/src/utils/optionalPackages.ts +++ b/packages/cli/src/utils/optionalPackages.ts @@ -136,7 +136,7 @@ export function loadBesideCli(name: OptionalPackage, cliUrl = import.meta.url): return pinnedCopyBesideCli(name, cliUrl) ? createRequire(cliUrl)(name) : null; } -function runNpm(args: string[]): Promise { +export function runNpm(args: string[]): Promise { const npm = buildNpmCommand(args); const child = spawn(npm.command, npm.args, { stdio: ["ignore", "pipe", "pipe"] }); return new Promise((resolve, reject) => { diff --git a/packages/cli/src/whisper/sherpa.test.ts b/packages/cli/src/whisper/sherpa.test.ts index b3c0fd3346..c1c48a6a09 100644 --- a/packages/cli/src/whisper/sherpa.test.ts +++ b/packages/cli/src/whisper/sherpa.test.ts @@ -4,7 +4,13 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { DownloadOptions } from "../cloud/download.js"; -import { ensureParakeetModel, sherpaUnsupportedReason, type ModelFile } from "./sherpa.js"; +import { + ensureParakeetModel, + installSherpaRuntime, + sherpaPlatformPackage, + sherpaUnsupportedReason, + type ModelFile, +} from "./sherpa.js"; const file = (name: string, content: string): ModelFile => ({ name, @@ -22,6 +28,26 @@ function fakeDownload(served: Record) { }); } +describe("installSherpaRuntime", () => { + it.each([ + ["darwin", "arm64", "sherpa-onnx-darwin-arm64@1.13.8"], + ["linux", "x64", "sherpa-onnx-linux-x64@1.13.8"], + ["win32", "ia32", "sherpa-onnx-win-ia32@1.13.8"], + ])("pins the %s-%s native package", (platform, arch, spec) => { + expect(sherpaPlatformPackage(platform as NodeJS.Platform, arch)).toBe(spec); + }); + + it("asks npm for the runtime and its native package at the same exact version", async () => { + const run = vi.fn(async () => { + throw new Error("stop before touching the cache"); + }); + await installSherpaRuntime(run).catch(() => undefined); + const args = run.mock.calls[0]?.[0] as string[] | undefined; + expect(args).toContain("sherpa-onnx-node@1.13.8"); + expect(args).toContain(sherpaPlatformPackage()); + }); +}); + describe("ensureParakeetModel", () => { let dir: string; afterEach(() => rmSync(dir, { recursive: true, force: true })); diff --git a/packages/cli/src/whisper/sherpa.ts b/packages/cli/src/whisper/sherpa.ts index 4e0bb08981..54b45d4900 100644 --- a/packages/cli/src/whisper/sherpa.ts +++ b/packages/cli/src/whisper/sherpa.ts @@ -5,7 +5,7 @@ import { homedir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { downloadToFile } from "../cloud/download.js"; -import { CACHE_DIR, install, isInstalled } from "../utils/optionalPackages.js"; +import { CACHE_DIR, install, isInstalled, runNpm } from "../utils/optionalPackages.js"; import { mergeWindowsToWords, SHERPA_ERROR_PREFIX, @@ -78,8 +78,14 @@ export function sherpaRuntimeInstalled(): boolean { return isInstalled(SHERPA_RUNTIME_DIR, RUNTIME); } -export function installSherpaRuntime(): Promise { - return install(SHERPA_RUNTIME_DIR, RUNTIME, RUNTIME_VERSION); +/** The native package, pinned too: the runtime's own optionalDependencies accept any 1.13.x. */ +export function sherpaPlatformPackage(platform = process.platform, arch = process.arch): string { + return `sherpa-onnx-${platform === "win32" ? "win" : platform}-${arch}@${RUNTIME_VERSION}`; +} + +export function installSherpaRuntime(run = runNpm): Promise { + const native = sherpaPlatformPackage(); + return install(SHERPA_RUNTIME_DIR, RUNTIME, RUNTIME_VERSION, (args) => run([...args, native])); } /** Sizes only: hashing 650 MB on every transcribe is too slow, and install already verified them. */