Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(desktop): leave the unused OCR engine out of the bundled services

Dongdong Chen679bb2a77ea8
diff --git a/apps/desktop/scripts/build-native-runtime.d.mts b/apps/desktop/scripts/build-native-runtime.d.mts
new file mode 100644
index 00000000..c0688f7a
--- /dev/null
+++ b/apps/desktop/scripts/build-native-runtime.d.mts
@@ -0,0 +1,9 @@
+export function copyExternals(
+ deployed: string,
+ output: string,
+ roots: string[],
+ exclude?: (name: string) => unknown,
+): Promise<{
+ packages: { package: string; version: string }[];
+ excluded: Record<string, string>;
+}>;
diff --git a/apps/desktop/scripts/build-native-runtime.mjs b/apps/desktop/scripts/build-native-runtime.mjs
index 278ef2ee..702e687f 100644
--- a/apps/desktop/scripts/build-native-runtime.mjs
+++ b/apps/desktop/scripts/build-native-runtime.mjs
@@ -108,6 +108,12 @@ const OPTIONAL_IMPORTS = new Set([
"utf-8-validate",
]);
+// Dependencies the services never load, left out with everything only they
+// need. officeparser imports tesseract.js (an OCR engine with about 40 MB of
+// wasm) only when a parse asks for OCR, and document-office-extract.ts
+// always passes `ocr: false`.
+const UNLOADED_PACKAGES = new Set(["tesseract.js"]);
+
export const runtimeDirectory = (target) =>
path.join(desktop, "dist/installer/native", `runtime-${target}`);
@@ -163,6 +169,7 @@ export async function copyExternals(
}
};
const visit = async (name, from, required) => {
+ if (UNLOADED_PACKAGES.has(name)) return;
const directory = find(name, from);
if (!directory) {
if (required)
diff --git a/apps/desktop/src/main/build-native-runtime.test.ts b/apps/desktop/src/main/build-native-runtime.test.ts
new file mode 100644
index 00000000..da63c6cc
--- /dev/null
+++ b/apps/desktop/src/main/build-native-runtime.test.ts
@@ -0,0 +1,57 @@
+import {
+ existsSync,
+ mkdirSync,
+ mkdtempSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
+import { tmpdir } from "node:os";
+import path from "node:path";
+
+import { afterEach, expect, it } from "vitest";
+
+import { copyExternals } from "../../scripts/build-native-runtime.mjs";
+
+const roots: string[] = [];
+afterEach(() => {
+ for (const root of roots.splice(0))
+ rmSync(root, { recursive: true, force: true });
+});
+
+// A hoisted node_modules as `pnpm deploy --config.node-linker=hoisted` writes
+// it, with officeparser and the dependencies its 8.2.0 manifest declares.
+const deployed = () => {
+ const root = mkdtempSync(path.join(tmpdir(), "build-native-runtime-"));
+ roots.push(root);
+ const add = (name: string, dependencies: Record<string, string> = {}) => {
+ const directory = path.join(root, "deploy/node_modules", name);
+ mkdirSync(directory, { recursive: true });
+ writeFileSync(
+ path.join(directory, "package.json"),
+ JSON.stringify({ name, version: "1.0.0", dependencies }),
+ );
+ writeFileSync(path.join(directory, "index.js"), "");
+ };
+ add("officeparser", { "pdfjs-dist": "1", "tesseract.js": "1" });
+ add("pdfjs-dist");
+ add("tesseract.js", { "tesseract.js-core": "1", "node-fetch": "1" });
+ add("tesseract.js-core");
+ add("node-fetch");
+ return root;
+};
+
+it("leaves out the OCR engine officeparser only loads with OCR on", async () => {
+ const root = deployed();
+ const output = path.join(root, "output");
+ const result = await copyExternals(path.join(root, "deploy"), output, [
+ "officeparser",
+ ]);
+ expect(result.packages.map((item) => item.package).sort()).toEqual([
+ "node_modules/officeparser",
+ "node_modules/pdfjs-dist",
+ ]);
+ expect(result.excluded).toEqual({});
+ expect(existsSync(path.join(output, "node_modules/officeparser"))).toBe(true);
+ for (const name of ["tesseract.js", "tesseract.js-core", "node-fetch"])
+ expect(existsSync(path.join(output, "node_modules", name))).toBe(false);
+});
diff --git a/docs/desktop-release.md b/docs/desktop-release.md
index c312bf1a..9ad48983 100644
--- a/docs/desktop-release.md
+++ b/docs/desktop-release.md
@@ -456,5 +456,7 @@ Node.js into its own folder, removing `com.apple.quarantine` on macOS;
packages and needs no registry. On Linux the services need glibc 2.35
(Ubuntu 22.04, Debian 12), like the PostgreSQL build; with an older glibc the
desktop leaves them out and Native installs from the network. Model data
-(Voice) is not bundled: Node Services downloads it on first use.
+(Voice) is not bundled: Node Services downloads it on first use. Dependencies
+the services never load are left out too: officeparser imports its OCR
+engine, tesseract.js, only for OCR, which document extraction never asks for.
Intel Macs have none of this and install from the network.