|
| 1 | +import { createWriteStream, existsSync, rmSync } from 'fs'; |
| 2 | +import { CommandRunner, SubCommand, InquirerService } from 'nest-commander'; |
| 3 | +import { resolve } from 'path'; |
| 4 | +import { HttpService } from '@nestjs/axios'; |
| 5 | +import { Presets, SingleBar } from 'cli-progress'; |
| 6 | +import decompress from 'decompress'; |
| 7 | + |
| 8 | +@SubCommand({ name: 'init', aliases: ['setup'] }) |
| 9 | +export class InitCommand extends CommandRunner { |
| 10 | + CORTEX_RELEASES_URL = 'https://api.github.com/repos/janhq/cortex/releases'; |
| 11 | + constructor( |
| 12 | + private readonly httpService: HttpService, |
| 13 | + private readonly inquirerService: InquirerService, |
| 14 | + ) { |
| 15 | + super(); |
| 16 | + } |
| 17 | + |
| 18 | + async run(input: string[], options?: any): Promise<void> { |
| 19 | + options = await this.inquirerService.ask('create-init-questions', options); |
| 20 | + |
| 21 | + await this.download(this.parseEngineFileName(options)); |
| 22 | + } |
| 23 | + |
| 24 | + download = async (engineFileName: string): Promise<any> => { |
| 25 | + const res = await this.httpService |
| 26 | + .get(this.CORTEX_RELEASES_URL) |
| 27 | + .toPromise(); |
| 28 | + |
| 29 | + if (!res?.data) { |
| 30 | + console.log('Failed to fetch releases'); |
| 31 | + return; |
| 32 | + } |
| 33 | + console.log(`Downloading engine file ${engineFileName}`); |
| 34 | + |
| 35 | + const releases = Array(res?.data); |
| 36 | + const toDownloadAsset = releases[0][0].assets.find((s: any) => |
| 37 | + s.name.includes(engineFileName), |
| 38 | + ); |
| 39 | + |
| 40 | + const engineDir = resolve(this.rootDir(), 'cortex-cpp'); |
| 41 | + if (existsSync(engineDir)) rmSync(engineDir, { recursive: true }); |
| 42 | + |
| 43 | + const download = await this.httpService |
| 44 | + .get(toDownloadAsset.browser_download_url, { |
| 45 | + responseType: 'stream', |
| 46 | + }) |
| 47 | + .toPromise(); |
| 48 | + if (!download) { |
| 49 | + throw new Error('Failed to download model'); |
| 50 | + } |
| 51 | + |
| 52 | + const destination = resolve(this.rootDir(), toDownloadAsset.name); |
| 53 | + |
| 54 | + await new Promise((resolve, reject) => { |
| 55 | + const writer = createWriteStream(destination); |
| 56 | + let receivedBytes = 0; |
| 57 | + const totalBytes = download.headers['content-length']; |
| 58 | + |
| 59 | + writer.on('finish', () => { |
| 60 | + bar.stop(); |
| 61 | + resolve(true); |
| 62 | + }); |
| 63 | + |
| 64 | + writer.on('error', (error) => { |
| 65 | + bar.stop(); |
| 66 | + reject(error); |
| 67 | + }); |
| 68 | + |
| 69 | + const bar = new SingleBar({}, Presets.shades_classic); |
| 70 | + bar.start(100, 0); |
| 71 | + |
| 72 | + download.data.on('data', (chunk: any) => { |
| 73 | + receivedBytes += chunk.length; |
| 74 | + bar.update(Math.floor((receivedBytes / totalBytes) * 100)); |
| 75 | + }); |
| 76 | + |
| 77 | + download.data.pipe(writer); |
| 78 | + }); |
| 79 | + |
| 80 | + try { |
| 81 | + await decompress( |
| 82 | + resolve(this.rootDir(), destination), |
| 83 | + resolve(this.rootDir()), |
| 84 | + ); |
| 85 | + } catch (e) { |
| 86 | + console.log(e); |
| 87 | + } |
| 88 | + process.exit(0); |
| 89 | + }; |
| 90 | + |
| 91 | + parseEngineFileName = (options: { |
| 92 | + runMode?: 'CPU' | 'GPU'; |
| 93 | + gpuType?: 'Nvidia' | 'Others (Vulkan)'; |
| 94 | + instructions?: 'AVX' | 'AVX2' | 'AVX-512' | undefined; |
| 95 | + cudaVersion?: '11' | '12'; |
| 96 | + }) => { |
| 97 | + const platform = |
| 98 | + process.platform === 'win32' |
| 99 | + ? 'windows' |
| 100 | + : process.platform === 'darwin' |
| 101 | + ? 'mac' |
| 102 | + : process.platform; |
| 103 | + const arch = process.arch === 'arm64' ? process.arch : 'amd64'; |
| 104 | + const cudaVersion = |
| 105 | + options.runMode === 'GPU' |
| 106 | + ? options.gpuType === 'Nvidia' |
| 107 | + ? '-cuda-' + (options.cudaVersion === '11' ? '11.7' : '12.2') |
| 108 | + : '-vulkan' |
| 109 | + : ''; |
| 110 | + const instructions = options.instructions ? `-${options.instructions}` : ''; |
| 111 | + const engineName = `${platform}-${arch}${instructions.toLowerCase()}${cudaVersion}`; |
| 112 | + return `${engineName}.tar.gz`; |
| 113 | + }; |
| 114 | + |
| 115 | + rootDir = () => resolve(__dirname, `../../../`); |
| 116 | +} |
0 commit comments