-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathmodelsFolder.ts
More file actions
68 lines (66 loc) · 2.34 KB
/
Copy pathmodelsFolder.ts
File metadata and controls
68 lines (66 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { type SimpleLogger } from "@lmstudio/lms-common";
import { findLMStudioHome } from "@lmstudio/lms-common-server";
import { access, mkdir, readFile } from "fs/promises";
import { join } from "path";
import { defaultModelsFolder } from "./lmstudioPaths.js";
/**
* Locate the settings.json file of LM Studio.
*
* @param logger - The logger to use.
* @returns A promise that resolves with the path to the settings.json file, or null if it does not
* exist.
*/
export async function locateSettingsJson(logger: SimpleLogger) {
logger.debug("Locating settings.json");
const lmstudioHome = findLMStudioHome();
const settingsJsonFilePath = join(lmstudioHome, "settings.json");
logger.debug("Settings.json file path", settingsJsonFilePath);
try {
await access(settingsJsonFilePath);
return settingsJsonFilePath;
} catch (error) {
logger.debug("settings.json does not exist", error);
return null;
}
}
/**
* Resolve the path to the models folder. If the settings.json file exists, use the downloadsFolder
* field. Otherwise, fall back to the default models folder.
*
* @param logger - The logger to use.
* @param opts - Options. Set `ensureExists` to `false` to skip creating the folder if it does not
* exist (useful for read-only operations such as listing or removing models).
* @returns A promise that resolves with the path to the models folder.
*/
export async function resolveModelsFolderPath(
logger: SimpleLogger,
{ ensureExists = true }: { ensureExists?: boolean } = {},
) {
const settingsJsonPath = await locateSettingsJson(logger);
let modelsFolderPath = defaultModelsFolder;
if (settingsJsonPath === null) {
logger.warn(
"Could not locate LM Studio configuration file, using default path:",
modelsFolderPath,
);
} else {
try {
const content = await readFile(settingsJsonPath, "utf8");
const settings = JSON.parse(content);
modelsFolderPath = settings.downloadsFolder;
if (typeof modelsFolderPath !== "string") {
throw new Error("downloadsFolder is not a string");
}
} catch (error) {
logger.warn(
"Could not parse LM Studio configuration file, using default path:",
modelsFolderPath,
);
logger.debug(error);
}
}
if (ensureExists) {
await mkdir(modelsFolderPath, { recursive: true });
}
return modelsFolderPath;
}