UNPKG

alwaysai

Version:

The alwaysAI command-line interface (CLI)

79 lines 3.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.modelPackageCloudClient = void 0; const path_1 = require("path"); const fs_1 = require("fs"); const cloud_api_1 = require("@alwaysai/cloud-api"); const alwayscli_1 = require("@alwaysai/alwayscli"); const tar = require("tar"); const tempy = require("tempy"); const util_1 = require("util"); const model_json_file_1 = require("./model-json-file"); const util_2 = require("../../util"); const infrastructure_1 = require("../../infrastructure"); const model_id_1 = require("./model-id"); const fetch_1 = require("../../util/fetch"); const stream_1 = require("stream"); async function getModelVersion(id, version) { try { return await (0, infrastructure_1.CliRpcClient)().getModelVersion({ id, version }); } catch (exception) { util_2.logger.error((0, util_2.stringifyError)(exception)); throw new alwayscli_1.CliTerseError(`Model ${id} not found!`); } } exports.modelPackageCloudClient = { async download(id, version) { const { uuid } = await getModelVersion(id, version); try { const modelPackageUrl = ModelPackageUrl(uuid); const authorizationHeader = await (0, infrastructure_1.CliAuthenticationClient)().getAuthorizationHeader(); const filestream = await (0, fetch_1.fetchFilestream)(modelPackageUrl, { method: 'GET', headers: Object.assign(Object.assign({}, authorizationHeader), { 'Content-Length': '0' }) }); return filestream; } catch (exception) { util_2.logger.error((0, util_2.stringifyError)(exception)); throw new alwayscli_1.CliTerseError(`Failed to download model ${id}!`); } }, async publish(dir = process.cwd()) { const modelJson = (0, model_json_file_1.ModelJsonFile)(dir).read(); const rpcClient = (0, infrastructure_1.CliRpcClient)(); const { uuid } = await rpcClient.createModelVersion(modelJson); const authorizationHeader = await (0, infrastructure_1.CliAuthenticationClient)().getAuthorizationHeader(); // Ensure top level directory matches model name const { name } = model_id_1.ModelId.parse(modelJson.id); const tmpDir = tempy.directory({ prefix: 'alwaysai' }); const tmpModelDir = (0, path_1.join)(tmpDir, name); await (0, util_2.JsSpawner)().mkdirp(tmpModelDir); await (0, util_2.copyFiles)((0, util_2.JsSpawner)({ path: dir }), (0, util_2.JsSpawner)({ path: tmpModelDir })); const modelPackagePath = tempy.file(); await tar.create({ cwd: tmpDir, gzip: true, file: modelPackagePath }, [ name ]); await (0, util_2.JsSpawner)().rimraf(tmpDir); const stats = await (0, util_1.promisify)(fs_1.stat)(modelPackagePath); const modelPackageStream = (0, fs_1.createReadStream)(modelPackagePath); // Convert to web stream since Typescript maps to DOM types at compile time, // which don't include all supported runtime types. const webModelPackageStream = stream_1.Readable.toWeb(modelPackageStream); const response = await fetch(ModelPackageUrl(uuid), { method: 'PUT', headers: Object.assign({ 'Content-Type': 'application/gzip', 'Content-Length': stats.size.toString() }, authorizationHeader), body: webModelPackageStream, duplex: 'half' }); await (0, util_2.throwIfNotOk)(response); await rpcClient.finalizeModelVersion(uuid); return uuid; } }; function ModelPackageUrl(uuid) { const cloudApiUrl = (0, cloud_api_1.CloudApiUrl)((0, infrastructure_1.getSystemId)()); return `${cloudApiUrl}${cloud_api_1.CLOUD_API_MODEL_VERSION_PACKAGES_PATH}/${uuid}`; } //# sourceMappingURL=model-package-cloud-client.js.map