From 5dc16c6cefdfdb71fb50b16051e948d56a02715f Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 26 Jul 2024 15:38:15 +0700 Subject: [PATCH] chore: enhance error output (#923) --- cortex-js/src/command.module.ts | 2 + cortex-js/src/extensions/extensions.module.ts | 2 +- .../infrastructure/commanders/base.command.ts | 5 +- .../commanders/base.subcommand.ts | 30 ------------ .../commanders/benchmark.command.ts | 5 +- .../infrastructure/commanders/chat.command.ts | 7 +-- .../commanders/cortex-command.commander.ts | 2 - .../commanders/embeddings.command.ts | 5 +- .../commanders/engines.command.ts | 2 +- .../commanders/engines/engines-get.command.ts | 5 +- .../engines/engines-init.command.ts | 48 +++++++++---------- .../engines/engines-list.command.ts | 5 +- .../commanders/engines/engines-set.command.ts | 9 ++-- .../commanders/models.command.ts | 2 +- .../commanders/models/model-get.command.ts | 6 +-- .../commanders/models/model-list.command.ts | 5 +- .../commanders/models/model-pull.command.ts | 7 +-- .../commanders/models/model-remove.command.ts | 5 +- .../commanders/models/model-start.command.ts | 12 ++--- .../commanders/models/model-stop.command.ts | 6 +-- .../commanders/models/model-update.command.ts | 5 +- .../infrastructure/commanders/ps.command.ts | 13 +---- .../infrastructure/commanders/run.command.ts | 6 ++- .../services/cortex.client.module.ts | 9 ++++ .../commanders/services/cortex.client.ts | 20 ++++++++ .../file-manager/file-manager.service.ts | 4 +- .../src/usecases/cortex/cortex.usecases.ts | 12 ----- cortex-js/src/utils/logo.ts | 35 -------------- 28 files changed, 114 insertions(+), 160 deletions(-) delete mode 100644 cortex-js/src/infrastructure/commanders/base.subcommand.ts create mode 100644 cortex-js/src/infrastructure/commanders/services/cortex.client.module.ts create mode 100644 cortex-js/src/infrastructure/commanders/services/cortex.client.ts delete mode 100644 cortex-js/src/utils/logo.ts diff --git a/cortex-js/src/command.module.ts b/cortex-js/src/command.module.ts index 242136d07..fb35a5be9 100644 --- a/cortex-js/src/command.module.ts +++ b/cortex-js/src/command.module.ts @@ -28,6 +28,7 @@ import { EnginesListCommand } from './infrastructure/commanders/engines/engines- import { EnginesGetCommand } from './infrastructure/commanders/engines/engines-get.command'; import { EnginesInitCommand } from './infrastructure/commanders/engines/engines-init.command'; import { EnginesSetCommand } from './infrastructure/commanders/engines/engines-set.command'; +import { CortexClientModule } from './infrastructure/commanders/services/cortex.client.module'; @Module({ imports: [ @@ -41,6 +42,7 @@ import { EnginesSetCommand } from './infrastructure/commanders/engines/engines-s FileManagerModule, TelemetryModule, ContextModule, + CortexClientModule, ], providers: [ CortexCommand, diff --git a/cortex-js/src/extensions/extensions.module.ts b/cortex-js/src/extensions/extensions.module.ts index 22f806e7f..82dc78180 100644 --- a/cortex-js/src/extensions/extensions.module.ts +++ b/cortex-js/src/extensions/extensions.module.ts @@ -5,7 +5,7 @@ import OpenAIEngineExtension from './openai.engine'; import { HttpModule, HttpService } from '@nestjs/axios'; import { ConfigsUsecases } from '@/usecases/configs/configs.usecase'; import { ConfigsModule } from '@/usecases/configs/configs.module'; -import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter'; +import { EventEmitter2 } from '@nestjs/event-emitter'; import AnthropicEngineExtension from './anthropic.engine'; const provider = { diff --git a/cortex-js/src/infrastructure/commanders/base.command.ts b/cortex-js/src/infrastructure/commanders/base.command.ts index fb335a18d..9d7ac19fb 100644 --- a/cortex-js/src/infrastructure/commanders/base.command.ts +++ b/cortex-js/src/infrastructure/commanders/base.command.ts @@ -1,7 +1,6 @@ import { CommandRunner } from 'nest-commander'; import { Injectable } from '@nestjs/common'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; -import Cortex from '@cortexso/cortex.js'; import ora from 'ora'; @Injectable() @@ -21,7 +20,9 @@ export abstract class BaseCommand extends CommandRunner { const checkingSpinner = ora('Checking API server online...').start(); const result = await this.cortexUseCases.isAPIServerOnline(); if (!result) { - checkingSpinner.fail('API server is offline'); + checkingSpinner.fail( + 'API server is offline. Please run "cortex" before running this command', + ); process.exit(1); } checkingSpinner.succeed('API server is online'); diff --git a/cortex-js/src/infrastructure/commanders/base.subcommand.ts b/cortex-js/src/infrastructure/commanders/base.subcommand.ts deleted file mode 100644 index 2ce3e2878..000000000 --- a/cortex-js/src/infrastructure/commanders/base.subcommand.ts +++ /dev/null @@ -1,30 +0,0 @@ -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 268a3b10a..5bc1e47f7 100644 --- a/cortex-js/src/infrastructure/commanders/benchmark.command.ts +++ b/cortex-js/src/infrastructure/commanders/benchmark.command.ts @@ -16,7 +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'; +import { CortexClient } from './services/cortex.client'; @SubCommand({ name: 'benchmark', @@ -27,11 +27,12 @@ import { BaseSubCommand } from './base.subcommand'; description: 'Benchmark and analyze the performance of a specific AI model using a variety of system resources', }) -export class BenchmarkCommand extends BaseSubCommand { +export class BenchmarkCommand extends BaseCommand { constructor( private readonly cortexUsecases: CortexUsecases, private readonly fileService: FileManagerService, private readonly telemetryUsecases: TelemetryUsecases, + private readonly cortex: CortexClient, ) { super(cortexUsecases); } diff --git a/cortex-js/src/infrastructure/commanders/chat.command.ts b/cortex-js/src/infrastructure/commanders/chat.command.ts index 289c625ec..56ad0e8fd 100644 --- a/cortex-js/src/infrastructure/commanders/chat.command.ts +++ b/cortex-js/src/infrastructure/commanders/chat.command.ts @@ -17,7 +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'; +import { CortexClient } from './services/cortex.client'; type ChatOptions = { threadId?: string; @@ -37,7 +37,7 @@ type ChatOptions = { }, }) @SetCommandContext() -export class ChatCommand extends BaseSubCommand { +export class ChatCommand extends BaseCommand { chatClient: ChatClient; constructor( @@ -46,6 +46,7 @@ export class ChatCommand extends BaseSubCommand { private readonly fileService: FileManagerService, protected readonly cortexUsecases: CortexUsecases, protected readonly contextService: ContextService, + protected readonly cortex: CortexClient, ) { super(cortexUsecases); this.chatClient = new ChatClient(this.cortex); @@ -66,7 +67,7 @@ export class ChatCommand extends BaseSubCommand { // first input might be message input message = passedParams.length ? passedParams.join(' ') - : options.message ?? ''; + : (options.message ?? ''); // If model ID is not provided, prompt user to select from running models const { data: models } = await this.cortex.models.list(); if (models.length === 1) { diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index 601a090d8..563f08214 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -19,7 +19,6 @@ import { FileManagerService } from '../services/file-manager/file-manager.servic import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { ServeStopCommand } from './serve-stop.command'; import ora from 'ora'; -import { printSlogan } from '@/utils/logo'; import { EnginesSetCommand } from './engines/engines-set.command'; type ServeOptions = { @@ -77,7 +76,6 @@ export class CortexCommand extends CommandRunner { const showVersion = options?.version || false; const dataFolderPath = options?.dataFolder; if (showVersion) { - printSlogan(); console.log('\n'); console.log(`Cortex CLI - v${pkg.version}`); console.log(chalk.blue(`Github: ${pkg.homepage}`)); diff --git a/cortex-js/src/infrastructure/commanders/embeddings.command.ts b/cortex-js/src/infrastructure/commanders/embeddings.command.ts index 85de19c8b..aae6ba4e8 100644 --- a/cortex-js/src/infrastructure/commanders/embeddings.command.ts +++ b/cortex-js/src/infrastructure/commanders/embeddings.command.ts @@ -4,7 +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'; +import { CortexClient } from './services/cortex.client'; interface EmbeddingCommandOptions { encoding_format?: string; @@ -21,9 +21,10 @@ interface EmbeddingCommandOptions { 'Model to use for embedding. If not provided, it will prompt to select from running models.', }, }) -export class EmbeddingCommand extends BaseSubCommand { +export class EmbeddingCommand extends BaseCommand { constructor( private readonly inquirerService: InquirerService, + private readonly cortex: CortexClient, readonly cortexUsecases: CortexUsecases, ) { super(cortexUsecases); diff --git a/cortex-js/src/infrastructure/commanders/engines.command.ts b/cortex-js/src/infrastructure/commanders/engines.command.ts index 767df0ebf..5c4e39cf0 100644 --- a/cortex-js/src/infrastructure/commanders/engines.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines.command.ts @@ -61,7 +61,7 @@ export class EnginesCommand extends BaseCommand { ) { const commandInstance = this.moduleRef.get(commandClass, { strict: false }); if (commandInstance) { - await commandInstance.run(params, options); + await commandInstance.runCommand(params, options); } else { console.error('Command not found.'); } 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 b985cc3c6..7352a4c44 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts @@ -4,7 +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'; +import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: ' get', @@ -14,10 +14,11 @@ import { BaseSubCommand } from '../base.subcommand'; }, }) @SetCommandContext() -export class EnginesGetCommand extends BaseSubCommand { +export class EnginesGetCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, + private readonly cortex: CortexClient, ) { super(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 a1815e927..98daf0de2 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts @@ -8,7 +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'; +import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: ' init', @@ -18,11 +18,12 @@ import { BaseSubCommand } from '../base.subcommand'; }, }) @SetCommandContext() -export class EnginesInitCommand extends BaseSubCommand { +export class EnginesInitCommand extends BaseCommand { constructor( private readonly cortexUsecases: CortexUsecases, private readonly fileManagerService: FileManagerService, readonly contextService: ContextService, + private readonly cortex: CortexClient, ) { super(cortexUsecases); } @@ -47,29 +48,28 @@ export class EnginesInitCommand extends BaseSubCommand { await this.cortexUsecases.stopCortex(); } console.log(`Installing engine ${engine}...`); - await this.cortex.engines - .init(engine, params) - const response = await this.cortex.events.downloadEvent() - - const progressBar = new SingleBar({}, Presets.shades_classic); - progressBar.start(100, 0); - - for await (const stream of response) { - if (stream.length) { - const data = stream[0] as any; - if (data.status === 'downloaded') break; - let totalBytes = 0; - let totalTransferred = 0; - data.children.forEach((child: any) => { - totalBytes += child.size.total; - totalTransferred += child.size.transferred; - }); - progressBar.update(Math.floor((totalTransferred / totalBytes) * 100)); - } + await this.cortex.engines.init(engine, params); + const response = await this.cortex.events.downloadEvent(); + + const progressBar = new SingleBar({}, Presets.shades_classic); + progressBar.start(100, 0); + + for await (const stream of response) { + if (stream.length) { + const data = stream[0] as any; + if (data.status === 'downloaded') break; + let totalBytes = 0; + let totalTransferred = 0; + data.children.forEach((child: any) => { + totalBytes += child.size.total; + totalTransferred += child.size.transferred; + }); + progressBar.update(Math.floor((totalTransferred / totalBytes) * 100)); } - progressBar.stop(); - console.log('Engine installed successfully'); - process.exit(0); + } + progressBar.stop(); + console.log('Engine installed successfully'); + process.exit(0); } @Option({ 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 5b2987790..776183a65 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts @@ -4,17 +4,18 @@ 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'; +import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'list', description: 'Get all cortex engines', }) @SetCommandContext() -export class EnginesListCommand extends BaseSubCommand { +export class EnginesListCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + private readonly cortex: CortexClient, ) { super(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 b6d14e782..07b367e4d 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-set.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-set.command.ts @@ -2,7 +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'; +import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: ' set ', @@ -12,8 +12,11 @@ import { BaseSubCommand } from '../base.subcommand'; }, }) @SetCommandContext() -export class EnginesSetCommand extends BaseSubCommand { - constructor(readonly cortexUsecases: CortexUsecases) { +export class EnginesSetCommand extends BaseCommand { + constructor( + readonly cortexUsecases: CortexUsecases, + private readonly cortex: CortexClient, + ) { super(cortexUsecases); } diff --git a/cortex-js/src/infrastructure/commanders/models.command.ts b/cortex-js/src/infrastructure/commanders/models.command.ts index d06866a29..ab3f562b4 100644 --- a/cortex-js/src/infrastructure/commanders/models.command.ts +++ b/cortex-js/src/infrastructure/commanders/models.command.ts @@ -67,7 +67,7 @@ export class ModelsCommand extends BaseCommand { ) { const commandInstance = this.moduleRef.get(commandClass, { strict: false }); if (commandInstance) { - await commandInstance.run(params, options); + await commandInstance.runCommand(params, options); } else { console.error('Command not found.'); } 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 f044b013a..ea9fb61e0 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-get.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-get.command.ts @@ -1,10 +1,9 @@ import { SubCommand } from 'nest-commander'; -import { exit } from 'node:process'; 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'; +import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'get', @@ -15,10 +14,11 @@ import { BaseSubCommand } from '../base.subcommand'; }, }) @SetCommandContext() -export class ModelGetCommand extends BaseSubCommand { +export class ModelGetCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + private readonly cortex: CortexClient, ) { super(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 953d81c25..050b0e1e1 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-list.command.ts @@ -3,17 +3,18 @@ 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'; +import { CortexClient } from '../services/cortex.client'; interface ModelListOptions { format: 'table' | 'json'; } @SubCommand({ name: 'list', description: 'List all models locally.' }) @SetCommandContext() -export class ModelListCommand extends BaseSubCommand { +export class ModelListCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + private readonly cortex: CortexClient, ) { super(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 c87dbdd5f..8844abc52 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts @@ -16,7 +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'; +import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'pull', @@ -27,11 +27,12 @@ import { BaseSubCommand } from '../base.subcommand'; 'Download a model from a registry. Working with HuggingFace repositories. For available models, please visit https://huggingface.co/cortexso', }) @SetCommandContext() -export class ModelPullCommand extends BaseSubCommand { +export class ModelPullCommand extends BaseCommand { constructor( private readonly fileService: FileManagerService, - readonly contextService: ContextService, private readonly telemetryUsecases: TelemetryUsecases, + private readonly cortex: CortexClient, + readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, ) { super(cortexUsecases); 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 93ec2b290..e38f555df 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts @@ -4,7 +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'; +import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'remove', @@ -15,10 +15,11 @@ import { BaseSubCommand } from '../base.subcommand'; }, }) @SetCommandContext() -export class ModelRemoveCommand extends BaseSubCommand { +export class ModelRemoveCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + private readonly cortex: CortexClient, ) { super(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 928be6b38..607b644b0 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-start.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-start.command.ts @@ -4,12 +4,7 @@ import { exit } from 'node:process'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { SetCommandContext } from '../decorators/CommandContext'; import { ContextService } from '@/infrastructure/services/context/context.service'; -import { - createReadStream, - existsSync, - statSync, - watchFile, -} from 'node:fs'; +import { createReadStream, existsSync, statSync, watchFile } from 'node:fs'; import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { join } from 'node:path'; import { Engines } from '../types/engine.interface'; @@ -17,7 +12,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'; +import { CortexClient } from '../services/cortex.client'; type ModelStartOptions = { attach: boolean; @@ -33,12 +28,13 @@ type ModelStartOptions = { }, }) @SetCommandContext() -export class ModelStartCommand extends BaseSubCommand { +export class ModelStartCommand extends BaseCommand { constructor( private readonly inquirerService: InquirerService, readonly cortexUsecases: CortexUsecases, private readonly fileService: FileManagerService, readonly contextService: ContextService, + private readonly cortex: CortexClient, ) { super(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 e99710b42..77377f53e 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts @@ -1,11 +1,10 @@ import { SubCommand } from 'nest-commander'; -import { exit } from 'node:process'; 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 ora from 'ora'; -import { BaseSubCommand } from '../base.subcommand'; +import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'stop', @@ -16,10 +15,11 @@ import { BaseSubCommand } from '../base.subcommand'; }, }) @SetCommandContext() -export class ModelStopCommand extends BaseSubCommand { +export class ModelStopCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + private readonly cortex: CortexClient, ) { super(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 af766c4b1..293bdfd03 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-update.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-update.command.ts @@ -5,7 +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'; +import { CortexClient } from '../services/cortex.client'; type UpdateOptions = { model?: string; @@ -21,10 +21,11 @@ type UpdateOptions = { }, }) @SetCommandContext() -export class ModelUpdateCommand extends BaseSubCommand { +export class ModelUpdateCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + private readonly cortex: CortexClient, ) { super(cortexUseCases); } diff --git a/cortex-js/src/infrastructure/commanders/ps.command.ts b/cortex-js/src/infrastructure/commanders/ps.command.ts index c6d46065d..1e4adb3a4 100644 --- a/cortex-js/src/infrastructure/commanders/ps.command.ts +++ b/cortex-js/src/infrastructure/commanders/ps.command.ts @@ -30,20 +30,11 @@ export class PSCommand extends BaseCommand { } async runCommand(): Promise { const runningSpinner = ora('Running PS command...').start(); - let checkingSpinner: ora.Ora; return this.getModels() .then((models: ModelStat[]) => { runningSpinner.succeed(); - console.table(models); - }) - .then(() => { - checkingSpinner = ora('Checking API server...').start(); - return this.cortexUsecases.isAPIServerOnline(); - }) - .then((isOnline) => { - checkingSpinner.succeed( - isOnline ? 'API server is online' : 'API server is offline', - ); + if (models.length) console.table(models); + else console.log('No models running'); }) .then(async () => { const cpuUsage = ( diff --git a/cortex-js/src/infrastructure/commanders/run.command.ts b/cortex-js/src/infrastructure/commanders/run.command.ts index 266a29b53..e65e78723 100644 --- a/cortex-js/src/infrastructure/commanders/run.command.ts +++ b/cortex-js/src/infrastructure/commanders/run.command.ts @@ -11,7 +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'; +import { CortexClient } from './services/cortex.client'; type RunOptions = { threadId?: string; @@ -28,12 +28,13 @@ type RunOptions = { }, description: 'Shortcut to start a model and chat', }) -export class RunCommand extends BaseSubCommand { +export class RunCommand extends BaseCommand { chatClient: ChatClient; constructor( protected readonly cortexUsecases: CortexUsecases, private readonly inquirerService: InquirerService, private readonly fileService: FileManagerService, + private readonly cortex: CortexClient, ) { super(cortexUsecases); @@ -92,6 +93,7 @@ export class RunCommand extends BaseSubCommand { .then(() => { startingSpinner.succeed('Model loaded'); if (options.chat) this.chatClient.chat(modelId, options.threadId); + else console.log("To start a chat session, use the '--chat' flag"); }) .catch((e) => { startingSpinner.fail(e.message ?? e); diff --git a/cortex-js/src/infrastructure/commanders/services/cortex.client.module.ts b/cortex-js/src/infrastructure/commanders/services/cortex.client.module.ts new file mode 100644 index 000000000..8a363ee27 --- /dev/null +++ b/cortex-js/src/infrastructure/commanders/services/cortex.client.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { CortexClient } from './cortex.client'; + +@Module({ + imports: [], + providers: [CortexClient], + exports: [CortexClient], +}) +export class CortexClientModule {} diff --git a/cortex-js/src/infrastructure/commanders/services/cortex.client.ts b/cortex-js/src/infrastructure/commanders/services/cortex.client.ts new file mode 100644 index 000000000..d8f31de2a --- /dev/null +++ b/cortex-js/src/infrastructure/commanders/services/cortex.client.ts @@ -0,0 +1,20 @@ +import { + cortexNamespace, + cortexServerAPI, +} from '@/infrastructure/constants/cortex'; +import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +import Cortex from '@cortexso/cortex.js'; + +export class CortexClient extends Cortex { + serverConfigs: { host: string; port: number }; + + constructor() { + const fileManagerService: FileManagerService = new FileManagerService(); + const configs = fileManagerService.getServerConfig(); + super({ + baseURL: cortexServerAPI(configs.host, configs.port), + apiKey: cortexNamespace, + }); + this.serverConfigs = configs; + } +} 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 d5b7021f1..ee9871a7a 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 @@ -342,8 +342,8 @@ export class FileManagerService { config = (yaml.load(content) as Config) ?? this.defaultConfig(); } catch {} return { - host: this.defaultConfig().apiServerHost ?? 'localhost', - port: this.defaultConfig().apiServerPort ?? 1337, + host: config.apiServerHost ?? 'localhost', + port: config.apiServerPort ?? 1337, }; } } diff --git a/cortex-js/src/usecases/cortex/cortex.usecases.ts b/cortex-js/src/usecases/cortex/cortex.usecases.ts index fa9ab2d2a..cc3cd6315 100644 --- a/cortex-js/src/usecases/cortex/cortex.usecases.ts +++ b/cortex-js/src/usecases/cortex/cortex.usecases.ts @@ -114,18 +114,6 @@ export class CortexUsecases { } } - /** - * Stop the API server - * @returns - */ - async stopServe(): Promise { - return fetch(CORTEX_JS_SYSTEM_URL(), { - method: 'DELETE', - }) - .then(() => {}) - .catch(() => {}); - } - /** * Check whether the Cortex CPP is healthy * @param host diff --git a/cortex-js/src/utils/logo.ts b/cortex-js/src/utils/logo.ts deleted file mode 100644 index 59eeac92a..000000000 --- a/cortex-js/src/utils/logo.ts +++ /dev/null @@ -1,35 +0,0 @@ -export const printSlogan = () => { - console.log( - ' ___ ___ ___ ___ ___ ', - ); - console.log( - ' / /\\ / /\\ / /\\ ___ / /\\ /__/| ', - ); - console.log( - ' / /:/ / /::\\ / /::\\ / /\\ / /:/_ | |:| ', - ); - console.log( - ' / /:/ / /:/\\:\\ / /:/\\:\\ / /:/ / /:/ /\\ | |:| ', - ); - console.log( - ' / /:/ ___ / /:/ \\:\\ / /:/~/:/ / /:/ / /:/ /:/_ __|__|:| ', - ); - console.log( - ' /__/:/ / /\\ /__/:/ \\__\\:\\ /__/:/ /:/___ / /::\\ /__/:/ /:/ /\\ /__/::::\\____', - ); - console.log( - ' \\ \\:\\ / /:/ \\ \\:\\ / /:/ \\ \\:\\/:::::/ /__/:/\\:\\ \\ \\:\\/:/ /:/ ~\\~~\\::::/', - ); - console.log( - ' \\ \\:\\ /:/ \\ \\:\\ /:/ \\ \\::/~~~~ \\__\\/ \\:\\ \\ \\::/ /:/ |~~|:|~~ ', - ); - console.log( - ' \\ \\:\\/:/ \\ \\:\\/:/ \\ \\:\\ \\ \\:\\ \\ \\:\\/:/ | |:| ', - ); - console.log( - ' \\ \\::/ \\ \\::/ \\ \\:\\ \\__\\/ \\ \\::/ | |:| ', - ); - console.log( - ' \\__\\/ \\__\\/ \\__\\/ \\__\\/ |__|/ ', - ); -};