alwaysai
Version:
The alwaysAI command-line interface (CLI)
85 lines • 3.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.appInstallModel = void 0;
const alwayscli_1 = require("@alwaysai/alwayscli");
const path_1 = require("path");
const model_1 = require("../model");
const util_1 = require("../../util");
const model_package_json_file_1 = require("../model/model-package-json-file");
const constants_1 = require("../../constants");
const paths_1 = require("../../paths");
async function appInstallModel(target, id, version) {
const { publisher, name } = model_1.ModelId.parse(id);
const destinationDir = path_1.posix.join(paths_1.APP_MODELS_DIRECTORY_NAME, publisher, name);
let installedVersion = undefined;
try {
const parsed = await readModelJson(destinationDir);
installedVersion = parsed.version;
}
catch (exception) {
util_1.logger.warn(`Failed to read existing model config: ${(0, util_1.stringifyError)(exception)}`);
// TODO finer-grained error handling
}
if (installedVersion !== version) {
const tmpId = (0, util_1.RandomString)();
const tmpDir = `${destinationDir}.${tmpId}.tmp`;
await target.mkdirp(tmpDir);
try {
if (!model_1.modelPackageCache.has(id, version)) {
await (0, model_1.downloadModelPackageToCache)(id, version);
}
try {
const modelPackageStream = model_1.modelPackageCache.read(id, version);
await target.untar(modelPackageStream, tmpDir);
}
catch (_a) {
try {
await model_1.modelPackageCache.remove(id, version);
}
catch (_b) {
throw new alwayscli_1.CliTerseError(`Failed to install model due to corrupt cache. If the command continues to fail you may need to run ${constants_1.ALWAYSAI_CLI_EXECUTABLE_NAME} model prune ${id} to remove the corrupt file`);
}
}
const fileNames = await target.readdir(tmpDir);
// Sanity check
if (fileNames.length !== 1 || !fileNames[0]) {
throw new Error('Expected package to contain single directory');
}
// The model json file in the package does not have the version number,
// so we write it into the json file during install
await updateModelJson(path_1.posix.join(tmpDir, fileNames[0]), (modelJson) => (Object.assign(Object.assign({}, modelJson), { version })));
await target.rimraf(destinationDir);
await target.mkdirp((0, path_1.dirname)(destinationDir));
await target.rename(target.resolvePath(tmpDir, fileNames[0]), destinationDir);
}
catch (exception) {
try {
// Attempt to delete new files since they may be corrupted
await target.rimraf(tmpDir);
await target.rimraf(destinationDir);
}
finally {
// Do nothing
}
throw exception;
}
finally {
await target.rimraf(tmpDir);
}
}
async function readModelJson(dir) {
const filePath = target.resolvePath(dir, model_package_json_file_1.MODEL_JSON_FILE_NAME);
const output = await target.readFile(filePath);
const parsed = JSON.parse(output);
return parsed;
}
async function updateModelJson(dir, updater) {
const parsed = await readModelJson(dir);
const updated = updater(parsed);
const filePath = target.resolvePath(dir, model_package_json_file_1.MODEL_JSON_FILE_NAME);
const serialized = JSON.stringify(updated, null, 2);
await target.writeFile(filePath, serialized);
}
}
exports.appInstallModel = appInstallModel;
//# sourceMappingURL=app-install-model.js.map