Lines 1-72javascript
2// Semantic runtime: makes local embeddings work after a normal install.
4// '@huggingface/transformers' is deliberately NOT a dependency of the npm
5// package — it would turn a 28MB install into a ~250MB one and slow the
6// `npx memwarden audit` funnel to a crawl. Instead `memwarden up` installs
7// it ONCE into <dataDir>/runtime (npm --prefix), and the embedding loader
8// falls back to that location when the bare specifier doesn't resolve.
9// Everything stays honest: if neither resolves, the daemon says
10// "lexical-only" instead of silently degrading.
11import { existsSync, readFileSync } from "node:fs";
12import { spawnSync } from "node:child_process";
13import { join } from "node:path";
HighChild Process
Package source references child process execution.
dist/embedding/runtime.jsView on unpkg · L11 14import { homedir } from "node:os";
15import { pathToFileURL } from "node:url";
16const PKG = "@huggingface/transformers";
17const PKG_SPEC = "@huggingface/transformers@^3.8.1";
18/** Where `memwarden up` installs the optional embedding runtime. */
19export function semanticRuntimeRoot(dataDir) {
20 const base = dataDir ?? process.env.MEMWARDEN_DATA_DIR ?? join(homedir(), ".memwarden");
21 return join(base, "runtime");
24 * Locate the transformers.js Node ESM entry inside a runtime root, without
25 * going through the exports map (package.json isn't an exported subpath).
26 * Returns an absolute file path, or null when not installed there.
28export function resolveTransformersEntry(root) {
29 const pkgDir = join(root, "node_modules", "@huggingface", "transformers");
30 const pkgJsonPath = join(pkgDir, "package.json");
31 if (!existsSync(pkgJsonPath))
34 const pkg = JSON.parse(readFileSync(pkgJsonPath, "utf8"));
35 const rel = pkg.exports?.node?.import?.default ?? pkg.module ?? pkg.main ?? null;
38 const entry = join(pkgDir, rel);
39 return existsSync(entry) ? entry : null;
46 * Import transformers.js: bare specifier first (dev checkouts, users who
47 * installed it themselves), then the memwarden runtime dir. Throws with an
48 * actionable message when neither resolves.
50export async function importTransformers() {
51 const specifier = PKG;
53 return (await import(specifier));
54 }
MediumDynamic Require
Package source references dynamic require/import behavior.
dist/embedding/runtime.jsView on unpkg · L52 56 // fall through to the runtime root
58 const entry = resolveTransformersEntry(semanticRuntimeRoot());
60 const mod = (await import(pathToFileURL(entry).href));
61 // Tolerate CJS/ESM interop: pipeline may sit on the default export.
62 if (typeof mod.pipeline === "function")
64 if (typeof mod.default?.pipeline === "function")
68 throw new Error(`Local embeddings require '${PKG}'. Run 'memwarden up' to install it ` +
69 `into ${semanticRuntimeRoot()}, or: npm install ${PKG}`);
72 * Install the embedding runtime under <dataDir>/runtime via npm --prefix.