fix(cli): make native modules (sharp, onnxruntime) optional + soften inspect overlap (#1476)

Aimed at `npx hyperframes` users (standalone and inside monorepos), where the
native modules `sharp` and `onnxruntime-node` can't install or load.

## Native modules are now optional, and never abort the CLI

`sharp` and `onnxruntime-node` are native modules: their platform binaries ship
as optional sub-dependencies that can fail to land on end-user installs
(--omit=optional, musl/glibc, monorepo hoisting, cross-platform lockfiles,
broken npx cache). Both powered only optional commands, yet both were wired as
hard `dependencies`, so on any platform where a binary can't install the whole
CLI failed to install. Moved both to `optionalDependencies` (alongside
@google/genai) so the core CLI always installs; the native-accelerated paths
light up only when present.

Runtime handling so a missing/unloadable binary degrades instead of crashing:

- `capture` (`contentExtractor.ts`): sharp was a static top-level
  `import sharp from "sharp"`, so a load failure threw on module import —
  before the inner try/catch — aborting the whole command. Now a guarded lazy
  `await import("sharp")` that skips SVG captioning with an actionable warning.
  Marked `external` in tsup so esbuild never bundles the native module.

- `remove-background` (`inference.ts`): both `onnxruntime-node` and `sharp`
  are loaded here and genuinely required. The dynamic imports are now guarded
  to throw an actionable "install / reinstall with optional deps" error
  (surfaced cleanly by the command's existing try/catch) instead of a raw
  "Cannot find module". New tests assert createSession rejects with that
  guidance — before touching the model download — when either module is
  unavailable.

`contactSheet.ts` also uses sharp but is already behind a dynamic-import
boundary wrapped in try/catch, so it was never a hard-fatal path.

## inspect: content-overlap as a warning, not a blocking error

The `content_overlap` layout-audit check shipped as `severity: "error"`, and
the audit exits non-zero when `errorCount > 0`, so `inspect` failed for
compositions that intentionally layer text. Downgraded to `severity: "warning"`
so it still reports (and prints the `data-layout-allow-overlap` opt-out hint)
without breaking exit codes. Reversible.
This commit is contained in:
Miguel Ángel
2026-06-15 20:15:52 -04:00
committed by GitHub
parent f03dfaa599
commit 8f71378185
6 changed files with 80 additions and 11 deletions
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { MEAN, STD, applyMask } from "./inference.js";
// Regression: the u2net_human_seg model was trained with ImageNet
@@ -125,3 +125,34 @@ describe("background-removal/inference — applyMask invariants", () => {
expect(bg[7]).toBe(0);
});
});
// onnxruntime-node and sharp are optional native modules; when their platform
// binary can't load, createSession must fail with an actionable install hint
// (and before touching the network / model download), not a raw module error.
describe("background-removal/inference — missing optional native modules", () => {
beforeEach(() => {
vi.resetModules();
});
it("createSession throws an actionable error when onnxruntime-node can't load", async () => {
vi.doMock("onnxruntime-node", () => {
throw new Error("Cannot find module 'onnxruntime-node'");
});
const { createSession } = await import("./inference.js");
await expect(createSession()).rejects.toThrow(
/onnxruntime-node.*isn't available[\s\S]*npm i onnxruntime-node/,
);
vi.doUnmock("onnxruntime-node");
});
it("createSession throws an actionable error when sharp can't load", async () => {
vi.doMock("onnxruntime-node", () => ({ InferenceSession: {}, Tensor: {} }));
vi.doMock("sharp", () => {
throw new Error("Could not load the sharp module");
});
const { createSession } = await import("./inference.js");
await expect(createSession()).rejects.toThrow(/sharp.*isn't available[\s\S]*npm i sharp/);
vi.doUnmock("onnxruntime-node");
vi.doUnmock("sharp");
});
});
@@ -52,10 +52,26 @@ export interface CreateSessionOptions {
onProgress?: (message: string) => void;
}
// onnxruntime-node and sharp are optional native modules — their platform
// binaries don't install everywhere. Surface an actionable error instead of a
// raw "Cannot find module" when one can't load.
async function loadNative<T>(name: string, load: () => Promise<T>): Promise<T> {
try {
return await load();
} catch (err) {
throw new Error(
`remove-background needs the optional native module '${name}', which isn't available ` +
`(${(err as Error).message}). Install it with \`npm i ${name}\`, or reinstall hyperframes with optional dependencies enabled.`,
);
}
}
export async function createSession(options: CreateSessionOptions = {}): Promise<Session> {
const ort = (await import("onnxruntime-node")) as unknown as OrtModule;
const sharpMod = await import("sharp");
const sharp = sharpMod.default as Sharp;
const ort = (await loadNative(
"onnxruntime-node",
() => import("onnxruntime-node"),
)) as unknown as OrtModule;
const sharp = (await loadNative("sharp", () => import("sharp"))).default as Sharp;
const choice = selectProviders(options.device ?? "auto");
const path = await ensureModel(options.model, { onProgress: options.onProgress });