From 572e1284cea46b282fc9ae8dcba77cca8a3a9b1a Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 26 Jul 2024 11:44:23 +0700 Subject: [PATCH 1/6] chore: update default api server config --- .../services/file-manager/file-manager.service.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts b/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts index da7a76900..d5b7021f1 100644 --- a/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts +++ b/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts @@ -17,6 +17,8 @@ import { createInterface } from 'readline'; import { defaultCortexCppHost, defaultCortexCppPort, + defaultCortexJsHost, + defaultCortexJsPort, } from '@/infrastructure/constants/cortex'; const readFileAsync = promisify(read); @@ -108,6 +110,8 @@ export class FileManagerService { dataFolderPath, cortexCppHost: defaultCortexCppHost, cortexCppPort: defaultCortexCppPort, + apiServerHost: defaultCortexJsHost, + apiServerPort: defaultCortexJsPort, }; } From 2fbf1df1e3d8cacb378875724d9e22051e2a51fe Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 26 Jul 2024 11:55:15 +0700 Subject: [PATCH 2/6] refactor: base command should not init cortex client --- .../infrastructure/commanders/base.command.ts | 19 ------------ .../commanders/base.subcommand.ts | 30 +++++++++++++++++++ .../commanders/benchmark.command.ts | 3 +- .../infrastructure/commanders/chat.command.ts | 3 +- .../commanders/embeddings.command.ts | 3 +- .../commanders/engines/engines-get.command.ts | 3 +- .../engines/engines-init.command.ts | 3 +- .../engines/engines-list.command.ts | 3 +- .../commanders/engines/engines-set.command.ts | 3 +- .../commanders/models/model-get.command.ts | 3 +- .../commanders/models/model-list.command.ts | 3 +- .../commanders/models/model-pull.command.ts | 3 +- .../commanders/models/model-remove.command.ts | 3 +- .../commanders/models/model-start.command.ts | 3 +- .../commanders/models/model-stop.command.ts | 3 +- .../commanders/models/model-update.command.ts | 3 +- .../infrastructure/commanders/run.command.ts | 3 +- .../src/infrastructure/constants/cortex.ts | 2 +- .../src/usecases/engines/engines.usecase.ts | 3 +- 19 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 cortex-js/src/infrastructure/commanders/base.subcommand.ts diff --git a/cortex-js/src/infrastructure/commanders/base.command.ts b/cortex-js/src/infrastructure/commanders/base.command.ts index 59354b4b4..fb335a18d 100644 --- a/cortex-js/src/infrastructure/commanders/base.command.ts +++ b/cortex-js/src/infrastructure/commanders/base.command.ts @@ -3,30 +3,11 @@ import { Injectable } from '@nestjs/common'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import Cortex from '@cortexso/cortex.js'; import ora from 'ora'; -import { FileManagerService } from '../services/file-manager/file-manager.service'; -import { cortexNamespace, cortexServerAPI } from '../constants/cortex'; @Injectable() export abstract class BaseCommand extends CommandRunner { - // Cortex client instance to communicate with cortex API server - cortex: Cortex; - serverConfigs: { host: string; port: number }; - constructor(readonly cortexUseCases: CortexUsecases) { super(); - // No need to inject services, since there is no nested dependencies - const fileManagerService: FileManagerService = new FileManagerService(); - this.serverConfigs = fileManagerService.getServerConfig(); - - // Instantiate cortex client, it will be use throughout the command - this.cortex = new Cortex({ - apiKey: cortexNamespace, - baseURL: cortexServerAPI( - this.serverConfigs.host, - this.serverConfigs.port, - ), - timeout: 20 * 1000, - }); } protected abstract runCommand( passedParam: string[], diff --git a/cortex-js/src/infrastructure/commanders/base.subcommand.ts b/cortex-js/src/infrastructure/commanders/base.subcommand.ts new file mode 100644 index 000000000..2ce3e2878 --- /dev/null +++ b/cortex-js/src/infrastructure/commanders/base.subcommand.ts @@ -0,0 +1,30 @@ +import { Injectable } from '@nestjs/common'; +import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; +import Cortex from '@cortexso/cortex.js'; +import { FileManagerService } from '../services/file-manager/file-manager.service'; +import { cortexNamespace, cortexServerAPI } from '../constants/cortex'; +import { BaseCommand } from './base.command'; + +@Injectable() +export abstract class BaseSubCommand extends BaseCommand { + // Cortex client instance to communicate with cortex API server + cortex: Cortex; + serverConfigs: { host: string; port: number }; + + constructor(readonly cortexUseCases: CortexUsecases) { + super(cortexUseCases); + // No need to inject services, since there is no nested dependencies + const fileManagerService: FileManagerService = new FileManagerService(); + this.serverConfigs = fileManagerService.getServerConfig(); + + // Instantiate cortex client, it will be use throughout the command + this.cortex = new Cortex({ + apiKey: cortexNamespace, + baseURL: cortexServerAPI( + this.serverConfigs.host, + this.serverConfigs.port, + ), + timeout: 20 * 1000, + }); + } +} diff --git a/cortex-js/src/infrastructure/commanders/benchmark.command.ts b/cortex-js/src/infrastructure/commanders/benchmark.command.ts index 447e37f03..268a3b10a 100644 --- a/cortex-js/src/infrastructure/commanders/benchmark.command.ts +++ b/cortex-js/src/infrastructure/commanders/benchmark.command.ts @@ -16,6 +16,7 @@ import { BenchmarkHardware } from '@/domain/telemetry/telemetry.interface'; import { defaultBenchmarkConfiguration } from '../constants/benchmark'; import { inspect } from 'util'; import { Cortex } from '@cortexso/cortex.js'; +import { BaseSubCommand } from './base.subcommand'; @SubCommand({ name: 'benchmark', @@ -26,7 +27,7 @@ import { Cortex } from '@cortexso/cortex.js'; description: 'Benchmark and analyze the performance of a specific AI model using a variety of system resources', }) -export class BenchmarkCommand extends BaseCommand { +export class BenchmarkCommand extends BaseSubCommand { constructor( private readonly cortexUsecases: CortexUsecases, private readonly fileService: FileManagerService, diff --git a/cortex-js/src/infrastructure/commanders/chat.command.ts b/cortex-js/src/infrastructure/commanders/chat.command.ts index 8128db193..289c625ec 100644 --- a/cortex-js/src/infrastructure/commanders/chat.command.ts +++ b/cortex-js/src/infrastructure/commanders/chat.command.ts @@ -17,6 +17,7 @@ import { isRemoteEngine } from '@/utils/normalize-model-id'; import { Cortex } from '@cortexso/cortex.js'; import { ChatClient } from './services/chat-client'; import { downloadModelProgress } from '@/utils/pull-model'; +import { BaseSubCommand } from './base.subcommand'; type ChatOptions = { threadId?: string; @@ -36,7 +37,7 @@ type ChatOptions = { }, }) @SetCommandContext() -export class ChatCommand extends BaseCommand { +export class ChatCommand extends BaseSubCommand { chatClient: ChatClient; constructor( diff --git a/cortex-js/src/infrastructure/commanders/embeddings.command.ts b/cortex-js/src/infrastructure/commanders/embeddings.command.ts index c15608b72..85de19c8b 100644 --- a/cortex-js/src/infrastructure/commanders/embeddings.command.ts +++ b/cortex-js/src/infrastructure/commanders/embeddings.command.ts @@ -4,6 +4,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { BaseCommand } from './base.command'; import { Cortex } from '@cortexso/cortex.js'; import ora from 'ora'; +import { BaseSubCommand } from './base.subcommand'; interface EmbeddingCommandOptions { encoding_format?: string; @@ -20,7 +21,7 @@ interface EmbeddingCommandOptions { 'Model to use for embedding. If not provided, it will prompt to select from running models.', }, }) -export class EmbeddingCommand extends BaseCommand { +export class EmbeddingCommand extends BaseSubCommand { constructor( private readonly inquirerService: InquirerService, readonly cortexUsecases: CortexUsecases, diff --git a/cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts b/cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts index 912087a6d..b985cc3c6 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts @@ -4,6 +4,7 @@ import { ContextService } from '@/infrastructure/services/context/context.servic import { EngineNamesMap, Engines } from '../types/engine.interface'; import { BaseCommand } from '../base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; +import { BaseSubCommand } from '../base.subcommand'; @SubCommand({ name: ' get', @@ -13,7 +14,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; }, }) @SetCommandContext() -export class EnginesGetCommand extends BaseCommand { +export class EnginesGetCommand extends BaseSubCommand { constructor( readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, diff --git a/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts b/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts index 186bb7faa..a1815e927 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts @@ -8,6 +8,7 @@ import { FileManagerService } from '@/infrastructure/services/file-manager/file- import { BaseCommand } from '../base.command'; import { defaultInstallationOptions } from '@/utils/init'; import { Presets, SingleBar } from 'cli-progress'; +import { BaseSubCommand } from '../base.subcommand'; @SubCommand({ name: ' init', @@ -17,7 +18,7 @@ import { Presets, SingleBar } from 'cli-progress'; }, }) @SetCommandContext() -export class EnginesInitCommand extends BaseCommand { +export class EnginesInitCommand extends BaseSubCommand { constructor( private readonly cortexUsecases: CortexUsecases, private readonly fileManagerService: FileManagerService, diff --git a/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts b/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts index 25f85949f..5b2987790 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts @@ -4,13 +4,14 @@ import { ContextService } from '@/infrastructure/services/context/context.servic import { EngineNamesMap } from '../types/engine.interface'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { BaseCommand } from '../base.command'; +import { BaseSubCommand } from '../base.subcommand'; @SubCommand({ name: 'list', description: 'Get all cortex engines', }) @SetCommandContext() -export class EnginesListCommand extends BaseCommand { +export class EnginesListCommand extends BaseSubCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, diff --git a/cortex-js/src/infrastructure/commanders/engines/engines-set.command.ts b/cortex-js/src/infrastructure/commanders/engines/engines-set.command.ts index 655c7d614..b6d14e782 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-set.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-set.command.ts @@ -2,6 +2,7 @@ import { SubCommand } from 'nest-commander'; import { SetCommandContext } from '../decorators/CommandContext'; import { BaseCommand } from '../base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; +import { BaseSubCommand } from '../base.subcommand'; @SubCommand({ name: ' set ', @@ -11,7 +12,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; }, }) @SetCommandContext() -export class EnginesSetCommand extends BaseCommand { +export class EnginesSetCommand extends BaseSubCommand { constructor(readonly cortexUsecases: CortexUsecases) { super(cortexUsecases); } diff --git a/cortex-js/src/infrastructure/commanders/models/model-get.command.ts b/cortex-js/src/infrastructure/commanders/models/model-get.command.ts index 7a8ff41e3..f044b013a 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-get.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-get.command.ts @@ -4,6 +4,7 @@ import { SetCommandContext } from '../decorators/CommandContext'; import { ContextService } from '@/infrastructure/services/context/context.service'; import { BaseCommand } from '../base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; +import { BaseSubCommand } from '../base.subcommand'; @SubCommand({ name: 'get', @@ -14,7 +15,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; }, }) @SetCommandContext() -export class ModelGetCommand extends BaseCommand { +export class ModelGetCommand extends BaseSubCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, diff --git a/cortex-js/src/infrastructure/commanders/models/model-list.command.ts b/cortex-js/src/infrastructure/commanders/models/model-list.command.ts index c83b072c7..953d81c25 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-list.command.ts @@ -3,13 +3,14 @@ import { SetCommandContext } from '../decorators/CommandContext'; import { ContextService } from '@/infrastructure/services/context/context.service'; import { BaseCommand } from '../base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; +import { BaseSubCommand } from '../base.subcommand'; interface ModelListOptions { format: 'table' | 'json'; } @SubCommand({ name: 'list', description: 'List all models locally.' }) @SetCommandContext() -export class ModelListCommand extends BaseCommand { +export class ModelListCommand extends BaseSubCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, diff --git a/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts b/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts index 48c62c161..c87dbdd5f 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts @@ -16,6 +16,7 @@ import { Engines } from '../types/engine.interface'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { BaseCommand } from '../base.command'; import { downloadModelProgress } from '@/utils/pull-model'; +import { BaseSubCommand } from '../base.subcommand'; @SubCommand({ name: 'pull', @@ -26,7 +27,7 @@ import { downloadModelProgress } from '@/utils/pull-model'; 'Download a model from a registry. Working with HuggingFace repositories. For available models, please visit https://huggingface.co/cortexso', }) @SetCommandContext() -export class ModelPullCommand extends BaseCommand { +export class ModelPullCommand extends BaseSubCommand { constructor( private readonly fileService: FileManagerService, readonly contextService: ContextService, diff --git a/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts b/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts index 117938163..93ec2b290 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts @@ -4,6 +4,7 @@ import { SetCommandContext } from '../decorators/CommandContext'; import { ContextService } from '@/infrastructure/services/context/context.service'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { BaseCommand } from '../base.command'; +import { BaseSubCommand } from '../base.subcommand'; @SubCommand({ name: 'remove', @@ -14,7 +15,7 @@ import { BaseCommand } from '../base.command'; }, }) @SetCommandContext() -export class ModelRemoveCommand extends BaseCommand { +export class ModelRemoveCommand extends BaseSubCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, diff --git a/cortex-js/src/infrastructure/commanders/models/model-start.command.ts b/cortex-js/src/infrastructure/commanders/models/model-start.command.ts index 422af9386..928be6b38 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-start.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-start.command.ts @@ -17,6 +17,7 @@ import { checkModelCompatibility } from '@/utils/model-check'; import { BaseCommand } from '../base.command'; import { isRemoteEngine } from '@/utils/normalize-model-id'; import { downloadModelProgress } from '@/utils/pull-model'; +import { BaseSubCommand } from '../base.subcommand'; type ModelStartOptions = { attach: boolean; @@ -32,7 +33,7 @@ type ModelStartOptions = { }, }) @SetCommandContext() -export class ModelStartCommand extends BaseCommand { +export class ModelStartCommand extends BaseSubCommand { constructor( private readonly inquirerService: InquirerService, readonly cortexUsecases: CortexUsecases, diff --git a/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts b/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts index 7a33d761b..e99710b42 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts @@ -5,6 +5,7 @@ import { ContextService } from '@/infrastructure/services/context/context.servic import { BaseCommand } from '../base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import ora from 'ora'; +import { BaseSubCommand } from '../base.subcommand'; @SubCommand({ name: 'stop', @@ -15,7 +16,7 @@ import ora from 'ora'; }, }) @SetCommandContext() -export class ModelStopCommand extends BaseCommand { +export class ModelStopCommand extends BaseSubCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, diff --git a/cortex-js/src/infrastructure/commanders/models/model-update.command.ts b/cortex-js/src/infrastructure/commanders/models/model-update.command.ts index a9d5f84ed..af766c4b1 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-update.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-update.command.ts @@ -5,6 +5,7 @@ import { UpdateModelDto } from '@/infrastructure/dtos/models/update-model.dto'; import { ContextService } from '@/infrastructure/services/context/context.service'; import { BaseCommand } from '../base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; +import { BaseSubCommand } from '../base.subcommand'; type UpdateOptions = { model?: string; @@ -20,7 +21,7 @@ type UpdateOptions = { }, }) @SetCommandContext() -export class ModelUpdateCommand extends BaseCommand { +export class ModelUpdateCommand extends BaseSubCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, diff --git a/cortex-js/src/infrastructure/commanders/run.command.ts b/cortex-js/src/infrastructure/commanders/run.command.ts index 9e9b3e86b..266a29b53 100644 --- a/cortex-js/src/infrastructure/commanders/run.command.ts +++ b/cortex-js/src/infrastructure/commanders/run.command.ts @@ -11,6 +11,7 @@ import { BaseCommand } from './base.command'; import { isRemoteEngine } from '@/utils/normalize-model-id'; import { ChatClient } from './services/chat-client'; import { downloadModelProgress } from '@/utils/pull-model'; +import { BaseSubCommand } from './base.subcommand'; type RunOptions = { threadId?: string; @@ -27,7 +28,7 @@ type RunOptions = { }, description: 'Shortcut to start a model and chat', }) -export class RunCommand extends BaseCommand { +export class RunCommand extends BaseSubCommand { chatClient: ChatClient; constructor( protected readonly cortexUsecases: CortexUsecases, diff --git a/cortex-js/src/infrastructure/constants/cortex.ts b/cortex-js/src/infrastructure/constants/cortex.ts index 1c364370c..77d59e6f2 100644 --- a/cortex-js/src/infrastructure/constants/cortex.ts +++ b/cortex-js/src/infrastructure/constants/cortex.ts @@ -61,4 +61,4 @@ export const CUDA_DOWNLOAD_URL = export const telemetryServerUrl = 'https://telemetry.jan.ai'; -export const MIN_CUDA_VERSION = '12.3'; +export const MIN_CUDA_VERSION = '12.4'; diff --git a/cortex-js/src/usecases/engines/engines.usecase.ts b/cortex-js/src/usecases/engines/engines.usecase.ts index c8d909dbd..ea601a42d 100644 --- a/cortex-js/src/usecases/engines/engines.usecase.ts +++ b/cortex-js/src/usecases/engines/engines.usecase.ts @@ -13,6 +13,7 @@ import { rm } from 'fs/promises'; import { CORTEX_ENGINE_RELEASES_URL, CUDA_DOWNLOAD_URL, + MIN_CUDA_VERSION, } from '@/infrastructure/constants/cortex'; import { DownloadManagerService } from '@/infrastructure/services/download-manager/download-manager.service'; @@ -177,7 +178,7 @@ export class EnginesUsecases { const dataFolderPath = await this.fileManagerService.getDataFolderPath(); const url = CUDA_DOWNLOAD_URL.replace( '', - cudaVersion === '11' ? '11.7' : '12.3', + cudaVersion === '11' ? '11.7' : MIN_CUDA_VERSION, ).replace('', platform); const destination = join(dataFolderPath, 'cuda-toolkit.tar.gz'); From df58d426180df926e1ded4ff85f36cc6eef970be Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 26 Jul 2024 12:12:59 +0700 Subject: [PATCH 3/6] fix: min cuda version for tensorrt support --- cortex-js/src/usecases/engines/engines.usecase.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cortex-js/src/usecases/engines/engines.usecase.ts b/cortex-js/src/usecases/engines/engines.usecase.ts index ea601a42d..8315cd29e 100644 --- a/cortex-js/src/usecases/engines/engines.usecase.ts +++ b/cortex-js/src/usecases/engines/engines.usecase.ts @@ -125,7 +125,11 @@ export class EnginesUsecases { options?.gpuType === 'Nvidia' && !options?.vulkan ) - await this.installCudaToolkitDependency(options?.cudaVersion); + await this.installCudaToolkitDependency( + engine === Engines.tensorrtLLM + ? MIN_CUDA_VERSION + : options?.cudaVersion, + ); // Update states await this.extensionRepository.findOne(engine).then((e) => { From 40ed7bca4aa08fa23348054f096de5283701c5c4 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Fri, 26 Jul 2024 12:12:33 +0700 Subject: [PATCH 4/6] chore: bump cortexjs package --- cortex-js/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cortex-js/package.json b/cortex-js/package.json index 210eb0e70..14d34831a 100644 --- a/cortex-js/package.json +++ b/cortex-js/package.json @@ -55,7 +55,7 @@ "class-validator": "^0.14.1", "cli-progress": "^3.12.0", "cortex-cpp": "0.4.34", - "@cortexso/cortex.js": "^0.1.1", + "@cortexso/cortex.js": "^0.1.2", "cpu-instructions": "^0.0.11", "decompress": "^4.2.1", "js-yaml": "^4.1.0", From ded52a983f26b899c791fbe980411d7feea42ca8 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Fri, 26 Jul 2024 12:13:06 +0700 Subject: [PATCH 5/6] fix: change pull endpoint, fix cortex command --- .../commanders/cortex-command.commander.ts | 22 ++++++++++++------- .../controllers/models.controller.ts | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index 26bed3b5c..60e283c79 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -49,6 +49,10 @@ type ServeOptions = { }) @SetCommandContext() export class CortexCommand extends CommandRunner { + host: string; + port: number; + configHost: string; + configPort: number; constructor( readonly contextService: ContextService, readonly fileManagerService: FileManagerService, @@ -58,8 +62,14 @@ export class CortexCommand extends CommandRunner { } async run(passedParams: string[], options?: ServeOptions): Promise { - const host = options?.address || defaultCortexJsHost; - const port = options?.port || defaultCortexJsPort; + const { + apiServerHost: configApiServerHost, + apiServerPort: configApiServerPort, + } = await this.fileManagerService.getConfig(); + this.configHost = configApiServerHost || defaultCortexJsHost; + this.configPort = configApiServerPort || defaultCortexJsPort; + this.host = options?.address || configApiServerHost || defaultCortexJsHost; + this.port = options?.port || configApiServerPort || defaultCortexJsPort; const showLogs = options?.logs || false; const showVersion = options?.version || false; const dataFolderPath = options?.dataFolder; @@ -70,7 +80,7 @@ export class CortexCommand extends CommandRunner { console.log(chalk.blue(`Github: ${pkg.homepage}`)); return; } - return this.startServer(host, port, showLogs, dataFolderPath); + return this.startServer(this.host, this.port, showLogs, dataFolderPath); } private async startServer( @@ -89,13 +99,9 @@ export class CortexCommand extends CommandRunner { startEngineSpinner.succeed('Cortex started successfully'); const isServerOnline = await this.cortexUseCases.isAPIServerOnline(); if (isServerOnline) { - const { - apiServerHost: configApiServerHost, - apiServerPort: configApiServerPort, - } = await this.fileManagerService.getConfig(); console.log( chalk.blue( - `Server is already running at http://${configApiServerHost}:${configApiServerPort}. Please use 'cortex stop' to stop the server.`, + `Server is already running at http://${this.configHost}:${this.configPort}. Please use 'cortex stop' to stop the server.`, ), ); process.exit(0); diff --git a/cortex-js/src/infrastructure/controllers/models.controller.ts b/cortex-js/src/infrastructure/controllers/models.controller.ts index 539c14e91..6287bcc5a 100644 --- a/cortex-js/src/infrastructure/controllers/models.controller.ts +++ b/cortex-js/src/infrastructure/controllers/models.controller.ts @@ -139,7 +139,7 @@ export class ModelsController { required: false, description: 'The unique identifier of the model in your local storage.', }) - @Post('pull/:modelId(*)') + @Post(':modelId(*)/pull') pullModel( @Param('modelId') modelId: string, @Body() body?: { From ded22bef85dac34890dfc98f48181a7355384117 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Fri, 26 Jul 2024 12:17:59 +0700 Subject: [PATCH 6/6] chore: refactor cortex command --- .../commanders/cortex-command.commander.ts | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index 60e283c79..11d228100 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -66,10 +66,13 @@ export class CortexCommand extends CommandRunner { apiServerHost: configApiServerHost, apiServerPort: configApiServerPort, } = await this.fileManagerService.getConfig(); + this.configHost = configApiServerHost || defaultCortexJsHost; this.configPort = configApiServerPort || defaultCortexJsPort; + this.host = options?.address || configApiServerHost || defaultCortexJsHost; this.port = options?.port || configApiServerPort || defaultCortexJsPort; + const showLogs = options?.logs || false; const showVersion = options?.version || false; const dataFolderPath = options?.dataFolder; @@ -80,12 +83,10 @@ export class CortexCommand extends CommandRunner { console.log(chalk.blue(`Github: ${pkg.homepage}`)); return; } - return this.startServer(this.host, this.port, showLogs, dataFolderPath); + return this.startServer(showLogs, dataFolderPath); } private async startServer( - host: string, - port: number, attach: boolean, dataFolderPath?: string, ) { @@ -116,18 +117,18 @@ export class CortexCommand extends CommandRunner { } if (attach) { const app = await getApp(); - await app.listen(port, host); + await app.listen(this.port, this.host); } else { - await this.cortexUseCases.startServerDetached(host, port); + await this.cortexUseCases.startServerDetached(this.host, this.port); } - console.log(chalk.blue(`Started server at http://${host}:${port}`)); + console.log(chalk.blue(`Started server at http://${this.host}:${this.port}`)); console.log( - chalk.blue(`API Playground available at http://${host}:${port}/api`), + chalk.blue(`API Playground available at http://${this.host}:${this.port}/api`), ); await this.fileManagerService.writeConfigFile({ ...config, - apiServerHost: host, - apiServerPort: port, + apiServerHost: this.host, + apiServerPort: this.port, dataFolderPath: dataFolderPath || config.dataFolderPath, }); if (!attach) process.exit(0); @@ -137,7 +138,7 @@ export class CortexCommand extends CommandRunner { await this.fileManagerService.writeConfigFile({ ...config, }); - console.error(`Failed to start server. Is port ${port} in use?`); + console.error(`Failed to start server. Is port ${this.port} in use?`); process.exit(1); } }