import { existsSync, mkdirSync, renameSync, rmSync } from "node:fs"; import { basename, join } from "node:path"; import type { EmbedderConfig } from "./config"; import { withFileLock } from "../store/lock"; export async function ensureModelFile(cacheDir: string, config: EmbedderConfig): Promise { mkdirSync(cacheDir, { recursive: true }); const destination = join(cacheDir, basename(config.modelFile)); return await withFileLock(`${destination}.lock`, async () => { if (existsSync(destination)) { return destination; } const temporary = `${destination}.tmp-${process.pid}`; const url = `https://huggingface.co/${config.modelRepo}/resolve/${config.modelRevision}/${config.modelFile}?download=true`; const download = Bun.spawn(["curl", "-L", "--fail", "--output", temporary, url], { stdout: "ignore", stderr: "pipe", }); const exitCode = await download.exited; if (exitCode !== 0) { const stderr = download.stderr instanceof ReadableStream ? await new Response(download.stderr).text() : ""; rmSync(temporary, { force: true }); throw new Error(`failed to download embedder model: ${stderr.trim()}`); } renameSync(temporary, destination); return destination; }); }