11import path from "path" ;
22import fs from "fs-extra" ;
33import chalk from "chalk" ;
4+ import { acquireLock } from "lifecycle-utils" ;
45import { cliModelsDirectory } from "../config.js" ;
56import { getReadablePath } from "../cli/utils/getReadablePath.js" ;
67import { resolveSplitGgufParts } from "../gguf/utils/resolveSplitGgufParts.js" ;
@@ -12,7 +13,6 @@ import {genericFilePartNumber} from "./parseModelUri.js";
1213import { isFilePartText } from "./parseModelFileName.js" ;
1314import { pushAll } from "./pushAll.js" ;
1415
15-
1616export type ResolveModelFileOptions = {
1717 /**
1818 * The directory to resolve models from, and download models to.
@@ -270,19 +270,29 @@ export async function resolveModelFile(
270270 }
271271 }
272272
273- if ( resolvedCli )
274- console . info ( `Downloading to ${ chalk . yellow ( getReadablePath ( resolvedDirectory ) ) } $ {
275- downloader . splitBinaryParts != null
276- ? chalk . gray ( ` (combining ${ downloader . splitBinaryParts } parts into a single file)` )
277- : ""
278- } ` ) ;
273+ const lock = await acquireLock ( [ resolveModelFile , "download" , downloader . entrypointFilePath ] ) ;
274+ try {
275+ if ( await fs . pathExists ( downloader . entrypointFilePath ) ) {
276+ await downloader . cancel ( { deleteTempFile : false } ) ;
277+ return downloader . entrypointFilePath ;
278+ }
279279
280- await downloader . download ( { signal} ) ;
280+ if ( resolvedCli )
281+ console . info ( `Downloading to ${ chalk . yellow ( getReadablePath ( resolvedDirectory ) ) } ${
282+ downloader . splitBinaryParts != null
283+ ? chalk . gray ( ` (combining ${ downloader . splitBinaryParts } parts into a single file)` )
284+ : ""
285+ } `) ;
281286
282- if ( resolvedCli )
283- console . info ( `Downloaded to ${ chalk . yellow ( getReadablePath ( downloader . entrypointFilePath ) ) } ` ) ;
287+ await downloader . download ( { signal} ) ;
284288
285- return downloader . entrypointFilePath ;
289+ if ( resolvedCli )
290+ console . info ( `Downloaded to ${ chalk . yellow ( getReadablePath ( downloader . entrypointFilePath ) ) } ` ) ;
291+
292+ return downloader . entrypointFilePath ;
293+ } finally {
294+ lock . dispose ( ) ;
295+ }
286296}
287297
288298async function findMatchingFilesInDirectory ( dirPath : string , fileNames : ( string | `${string } ${typeof genericFilePartNumber } ${string } `) [ ] ) {
0 commit comments