From 328d285f49a21ced297122a5d570138219e27653 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Fri, 2 Aug 2024 14:29:54 +0700 Subject: [PATCH 1/9] feat: support multi process in cortex --- cortex-js/src/command.module.ts | 2 -- .../infrastructure/commanders/base.command.ts | 12 +++++++++- .../commanders/benchmark.command.ts | 6 ++--- .../infrastructure/commanders/chat.command.ts | 4 +--- .../commanders/cortex-command.commander.ts | 9 ++++++++ .../commanders/embeddings.command.ts | 6 ++--- .../commanders/engines.command.ts | 4 +++- .../commanders/engines/engines-get.command.ts | 6 ++--- .../engines/engines-init.command.ts | 6 ++--- .../engines/engines-list.command.ts | 6 ++--- .../commanders/engines/engines-set.command.ts | 6 ++--- .../commanders/models.command.ts | 4 +++- .../commanders/models/model-get.command.ts | 6 ++--- .../commanders/models/model-list.command.ts | 6 ++--- .../commanders/models/model-pull.command.ts | 4 +--- .../commanders/models/model-remove.command.ts | 6 ++--- .../commanders/models/model-start.command.ts | 4 +--- .../commanders/models/model-stop.command.ts | 6 ++--- .../commanders/models/model-update.command.ts | 6 ++--- .../commanders/presets.command.ts | 2 +- .../infrastructure/commanders/ps.command.ts | 5 ++--- .../infrastructure/commanders/run.command.ts | 4 +--- .../commanders/serve-stop.command.ts | 8 +++++-- .../commanders/services/cortex.client.ts | 3 +-- .../commanders/telemetry.command.ts | 4 +++- .../file-manager/file-manager.service.ts | 22 ++++++++++++++----- 26 files changed, 91 insertions(+), 66 deletions(-) diff --git a/cortex-js/src/command.module.ts b/cortex-js/src/command.module.ts index fb35a5be9..242136d07 100644 --- a/cortex-js/src/command.module.ts +++ b/cortex-js/src/command.module.ts @@ -28,7 +28,6 @@ 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: [ @@ -42,7 +41,6 @@ import { CortexClientModule } from './infrastructure/commanders/services/cortex. FileManagerModule, TelemetryModule, ContextModule, - CortexClientModule, ], providers: [ CortexCommand, diff --git a/cortex-js/src/infrastructure/commanders/base.command.ts b/cortex-js/src/infrastructure/commanders/base.command.ts index 9d7ac19fb..3625df644 100644 --- a/cortex-js/src/infrastructure/commanders/base.command.ts +++ b/cortex-js/src/infrastructure/commanders/base.command.ts @@ -2,10 +2,18 @@ import { CommandRunner } from 'nest-commander'; import { Injectable } from '@nestjs/common'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import ora from 'ora'; +import { CortexClient } from './services/cortex.client'; +import Module from 'module'; +import { ModuleRef } from '@nestjs/core'; +import { FileManagerService } from '../services/file-manager/file-manager.service'; @Injectable() export abstract class BaseCommand extends CommandRunner { - constructor(readonly cortexUseCases: CortexUsecases) { + cortex: CortexClient; + constructor( + readonly cortexUseCases: CortexUsecases, + readonly fileManagerService: FileManagerService, + ) { super(); } protected abstract runCommand( @@ -26,6 +34,8 @@ export abstract class BaseCommand extends CommandRunner { process.exit(1); } checkingSpinner.succeed('API server is online'); + console.log(options, passedParam); + this.cortex = new CortexClient(this.fileManagerService); await this.runCommand(passedParam, options); } } diff --git a/cortex-js/src/infrastructure/commanders/benchmark.command.ts b/cortex-js/src/infrastructure/commanders/benchmark.command.ts index 5bc1e47f7..8c005a3f7 100644 --- a/cortex-js/src/infrastructure/commanders/benchmark.command.ts +++ b/cortex-js/src/infrastructure/commanders/benchmark.command.ts @@ -16,7 +16,6 @@ import { BenchmarkHardware } from '@/domain/telemetry/telemetry.interface'; import { defaultBenchmarkConfiguration } from '../constants/benchmark'; import { inspect } from 'util'; import { Cortex } from '@cortexso/cortex.js'; -import { CortexClient } from './services/cortex.client'; @SubCommand({ name: 'benchmark', @@ -29,12 +28,11 @@ import { CortexClient } from './services/cortex.client'; }) export class BenchmarkCommand extends BaseCommand { constructor( - private readonly cortexUsecases: CortexUsecases, + readonly cortexUsecases: CortexUsecases, private readonly fileService: FileManagerService, private readonly telemetryUsecases: TelemetryUsecases, - private readonly cortex: CortexClient, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); } async runCommand( diff --git a/cortex-js/src/infrastructure/commanders/chat.command.ts b/cortex-js/src/infrastructure/commanders/chat.command.ts index 260418d89..61335104d 100644 --- a/cortex-js/src/infrastructure/commanders/chat.command.ts +++ b/cortex-js/src/infrastructure/commanders/chat.command.ts @@ -17,7 +17,6 @@ import { isRemoteEngine } from '@/utils/normalize-model-id'; import { Cortex } from '@cortexso/cortex.js'; import { ChatClient } from './services/chat-client'; import { downloadProgress } from '@/utils/download-progress'; -import { CortexClient } from './services/cortex.client'; import { DownloadType } from '@/domain/models/download.interface'; type ChatOptions = { @@ -47,9 +46,8 @@ export class ChatCommand extends BaseCommand { private readonly fileService: FileManagerService, protected readonly cortexUsecases: CortexUsecases, protected readonly contextService: ContextService, - protected readonly cortex: CortexClient, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); this.chatClient = new ChatClient(this.cortex); } diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index 5afe01ca7..c61596188 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -181,4 +181,13 @@ export class CortexCommand extends CommandRunner { parseVersion() { return true; } + @Option({ + flags: '-n, --name ', + description: 'Name of the process', + }) + parseName(value: string) { + console.log(value); + this.fileManagerService.setConfigProfile(value); + return value; + } } diff --git a/cortex-js/src/infrastructure/commanders/embeddings.command.ts b/cortex-js/src/infrastructure/commanders/embeddings.command.ts index 61e78a824..941920c7e 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 { CortexClient } from './services/cortex.client'; +import { FileManagerService } from '../services/file-manager/file-manager.service'; interface EmbeddingCommandOptions { encoding_format?: string; @@ -24,10 +24,10 @@ interface EmbeddingCommandOptions { export class EmbeddingCommand extends BaseCommand { constructor( private readonly inquirerService: InquirerService, - private readonly cortex: CortexClient, readonly cortexUsecases: CortexUsecases, + private readonly fileService: FileManagerService, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); } async runCommand( passedParams: string[], diff --git a/cortex-js/src/infrastructure/commanders/engines.command.ts b/cortex-js/src/infrastructure/commanders/engines.command.ts index 9f3e84b82..da88c3928 100644 --- a/cortex-js/src/infrastructure/commanders/engines.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines.command.ts @@ -10,6 +10,7 @@ import { EngineNamesMap } from './types/engine.interface'; import { BaseCommand } from './base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { EnginesSetCommand } from './engines/engines-set.command'; +import { FileManagerService } from '../services/file-manager/file-manager.service'; @SubCommand({ name: 'engines', @@ -30,8 +31,9 @@ export class EnginesCommand extends BaseCommand { readonly contextService: ContextService, readonly moduleRef: ModuleRef, readonly cortexUsecases: CortexUsecases, + readonly fileService: FileManagerService, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); } async runCommand(passedParams: string[], options: { vulkan: boolean }) { const [parameter, command] = passedParams; 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 7352a4c44..01db3e8ec 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 { CortexClient } from '../services/cortex.client'; +import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: ' get', @@ -18,9 +18,9 @@ export class EnginesGetCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, - private readonly cortex: CortexClient, + readonly fileService: FileManagerService, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); } async runCommand(passedParams: string[]): Promise { 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 32c9047d4..3ae5c4627 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts @@ -7,7 +7,6 @@ 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 { CortexClient } from '../services/cortex.client'; import ora from 'ora'; import { InitEngineDto } from '@/infrastructure/dtos/engines/engines.dto'; @@ -22,11 +21,10 @@ import { InitEngineDto } from '@/infrastructure/dtos/engines/engines.dto'; export class EnginesInitCommand extends BaseCommand { constructor( private readonly cortexUsecases: CortexUsecases, - private readonly fileManagerService: FileManagerService, + readonly fileManagerService: FileManagerService, readonly contextService: ContextService, - private readonly cortex: CortexClient, ) { - super(cortexUsecases); + super(cortexUsecases, fileManagerService); } async runCommand( 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 776183a65..b565030ac 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts @@ -4,7 +4,7 @@ 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 { CortexClient } from '../services/cortex.client'; +import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: 'list', @@ -15,9 +15,9 @@ export class EnginesListCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - private readonly cortex: CortexClient, + readonly fileService: FileManagerService, ) { - super(cortexUseCases); + super(cortexUseCases, fileService); } async runCommand(): Promise { 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 07b367e4d..d4c1d70e7 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 { CortexClient } from '../services/cortex.client'; +import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: ' set ', @@ -15,9 +15,9 @@ import { CortexClient } from '../services/cortex.client'; export class EnginesSetCommand extends BaseCommand { constructor( readonly cortexUsecases: CortexUsecases, - private readonly cortex: CortexClient, + readonly fileService: FileManagerService, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); } async runCommand(passedParams: string[]): Promise { diff --git a/cortex-js/src/infrastructure/commanders/models.command.ts b/cortex-js/src/infrastructure/commanders/models.command.ts index ab3f562b4..777a63c44 100644 --- a/cortex-js/src/infrastructure/commanders/models.command.ts +++ b/cortex-js/src/infrastructure/commanders/models.command.ts @@ -9,6 +9,7 @@ import { BaseCommand } from './base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { ModuleRef } from '@nestjs/core'; import { ModelPullCommand } from './models/model-pull.command'; +import { FileManagerService } from '../services/file-manager/file-manager.service'; @SubCommand({ name: 'models', @@ -26,8 +27,9 @@ export class ModelsCommand extends BaseCommand { constructor( private readonly moduleRef: ModuleRef, readonly cortexUsecases: CortexUsecases, + readonly fileService: FileManagerService, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); } commandMap: { [key: string]: any } = { 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 ea9fb61e0..d6661c3df 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-get.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-get.command.ts @@ -3,7 +3,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 { CortexClient } from '../services/cortex.client'; +import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: 'get', @@ -18,9 +18,9 @@ export class ModelGetCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - private readonly cortex: CortexClient, + readonly fileService: FileManagerService, ) { - super(cortexUseCases); + super(cortexUseCases, fileService); } async runCommand(passedParams: string[]): Promise { 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 4a86f5e49..7acab8eda 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-list.command.ts @@ -3,7 +3,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 { CortexClient } from '../services/cortex.client'; +import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; interface ModelListOptions { format: 'table' | 'json'; @@ -14,9 +14,9 @@ export class ModelListCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - private readonly cortex: CortexClient, + readonly fileService: FileManagerService, ) { - super(cortexUseCases); + super(cortexUseCases, fileService); } async runCommand( 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 d88f52831..6bc99861c 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,6 @@ import { Engines } from '../types/engine.interface'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { BaseCommand } from '../base.command'; import { downloadProgress } from '@/utils/download-progress'; -import { CortexClient } from '../services/cortex.client'; import { DownloadType } from '@/domain/models/download.interface'; import ora from 'ora'; import { isRemoteEngine } from '@/utils/normalize-model-id'; @@ -34,11 +33,10 @@ export class ModelPullCommand extends BaseCommand { constructor( private readonly fileService: FileManagerService, private readonly telemetryUsecases: TelemetryUsecases, - private readonly cortex: CortexClient, readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); } async runCommand(passedParams: string[]) { 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 e38f555df..2ff010107 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 { CortexClient } from '../services/cortex.client'; +import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: 'remove', @@ -19,9 +19,9 @@ export class ModelRemoveCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - private readonly cortex: CortexClient, + readonly fileService: FileManagerService, ) { - super(cortexUseCases); + super(cortexUseCases, fileService); } async runCommand(passedParams: string[]): Promise { 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 0e4aabae2..5c61a7eec 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-start.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-start.command.ts @@ -12,7 +12,6 @@ import { checkModelCompatibility } from '@/utils/model-check'; import { BaseCommand } from '../base.command'; import { isRemoteEngine } from '@/utils/normalize-model-id'; import { downloadProgress } from '@/utils/download-progress'; -import { CortexClient } from '../services/cortex.client'; import { DownloadType } from '@/domain/models/download.interface'; import { printLastErrorLines } from '@/utils/logs'; @@ -36,9 +35,8 @@ export class ModelStartCommand extends BaseCommand { readonly cortexUsecases: CortexUsecases, private readonly fileService: FileManagerService, readonly contextService: ContextService, - private readonly cortex: CortexClient, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); } async runCommand( 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 77377f53e..aa4194f98 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts @@ -4,7 +4,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 { CortexClient } from '../services/cortex.client'; +import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: 'stop', @@ -19,9 +19,9 @@ export class ModelStopCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - private readonly cortex: CortexClient, + readonly fileService: FileManagerService, ) { - super(cortexUseCases); + super(cortexUseCases, fileService); } async runCommand(passedParams: string[]): Promise { 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 293bdfd03..7068ca42e 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 { CortexClient } from '../services/cortex.client'; +import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; type UpdateOptions = { model?: string; @@ -25,9 +25,9 @@ export class ModelUpdateCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - private readonly cortex: CortexClient, + readonly fileService: FileManagerService, ) { - super(cortexUseCases); + super(cortexUseCases, fileService); } async runCommand( diff --git a/cortex-js/src/infrastructure/commanders/presets.command.ts b/cortex-js/src/infrastructure/commanders/presets.command.ts index fca4e0489..b365d5f49 100644 --- a/cortex-js/src/infrastructure/commanders/presets.command.ts +++ b/cortex-js/src/infrastructure/commanders/presets.command.ts @@ -18,7 +18,7 @@ export class PresetCommand extends BaseCommand { readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); } async runCommand(): Promise { return console.table( diff --git a/cortex-js/src/infrastructure/commanders/ps.command.ts b/cortex-js/src/infrastructure/commanders/ps.command.ts index 1e4adb3a4..1dfa22add 100644 --- a/cortex-js/src/infrastructure/commanders/ps.command.ts +++ b/cortex-js/src/infrastructure/commanders/ps.command.ts @@ -21,12 +21,11 @@ import { CORTEX_CPP_MODELS_URL } from '../constants/cortex'; @SetCommandContext() export class PSCommand extends BaseCommand { constructor( - private readonly contextService: ContextService, - private readonly cortexUsecases: CortexUsecases, + readonly cortexUsecases: CortexUsecases, private readonly httpService: HttpService, private readonly fileService: FileManagerService, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); } async runCommand(): Promise { const runningSpinner = ora('Running PS command...').start(); diff --git a/cortex-js/src/infrastructure/commanders/run.command.ts b/cortex-js/src/infrastructure/commanders/run.command.ts index 1dcd88789..30a72ccdf 100644 --- a/cortex-js/src/infrastructure/commanders/run.command.ts +++ b/cortex-js/src/infrastructure/commanders/run.command.ts @@ -11,7 +11,6 @@ import { BaseCommand } from './base.command'; import { isRemoteEngine } from '@/utils/normalize-model-id'; import { ChatClient } from './services/chat-client'; import { downloadProgress } from '@/utils/download-progress'; -import { CortexClient } from './services/cortex.client'; import { DownloadType } from '@/domain/models/download.interface'; import { isLocalFile } from '@/utils/urls'; import { parse } from 'node:path'; @@ -38,9 +37,8 @@ export class RunCommand extends BaseCommand { protected readonly cortexUsecases: CortexUsecases, private readonly inquirerService: InquirerService, private readonly fileService: FileManagerService, - private readonly cortex: CortexClient, ) { - super(cortexUsecases); + super(cortexUsecases, fileService); this.chatClient = new ChatClient(this.cortex); } diff --git a/cortex-js/src/infrastructure/commanders/serve-stop.command.ts b/cortex-js/src/infrastructure/commanders/serve-stop.command.ts index ea9039a4d..4f2c7e0f1 100644 --- a/cortex-js/src/infrastructure/commanders/serve-stop.command.ts +++ b/cortex-js/src/infrastructure/commanders/serve-stop.command.ts @@ -1,14 +1,18 @@ import { SubCommand } from 'nest-commander'; import { BaseCommand } from './base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; +import { FileManagerService } from '../services/file-manager/file-manager.service'; @SubCommand({ name: 'stop', description: 'Stop the API server', }) export class ServeStopCommand extends BaseCommand { - constructor(private readonly cortexUsecases: CortexUsecases) { - super(cortexUsecases); + constructor( + private readonly cortexUsecases: CortexUsecases, + readonly fileService: FileManagerService, + ) { + super(cortexUsecases, fileService); } async runCommand(): Promise { return this.cortexUsecases diff --git a/cortex-js/src/infrastructure/commanders/services/cortex.client.ts b/cortex-js/src/infrastructure/commanders/services/cortex.client.ts index d8f31de2a..fa7ae4593 100644 --- a/cortex-js/src/infrastructure/commanders/services/cortex.client.ts +++ b/cortex-js/src/infrastructure/commanders/services/cortex.client.ts @@ -8,8 +8,7 @@ import Cortex from '@cortexso/cortex.js'; export class CortexClient extends Cortex { serverConfigs: { host: string; port: number }; - constructor() { - const fileManagerService: FileManagerService = new FileManagerService(); + constructor(fileManagerService: FileManagerService) { const configs = fileManagerService.getServerConfig(); super({ baseURL: cortexServerAPI(configs.host, configs.port), diff --git a/cortex-js/src/infrastructure/commanders/telemetry.command.ts b/cortex-js/src/infrastructure/commanders/telemetry.command.ts index b4f3b3ec5..7b966c895 100644 --- a/cortex-js/src/infrastructure/commanders/telemetry.command.ts +++ b/cortex-js/src/infrastructure/commanders/telemetry.command.ts @@ -4,6 +4,7 @@ import { TelemetryOptions } from './types/telemetry-options.interface'; import { SetCommandContext } from './decorators/CommandContext'; import { BaseCommand } from './base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; +import { FileManagerService } from '../services/file-manager/file-manager.service'; @SubCommand({ name: 'telemetry', @@ -14,8 +15,9 @@ export class TelemetryCommand extends BaseCommand { constructor( private readonly telemetryUseCase: TelemetryUsecases, readonly cortexUseCases: CortexUsecases, + readonly fileService: FileManagerService, ) { - super(cortexUseCases); + super(cortexUseCases, fileService); } async runCommand( 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 ee9871a7a..fc2151ea0 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 @@ -1,5 +1,5 @@ import { Config } from '@/domain/config/config.interface'; -import { Injectable } from '@nestjs/common'; +import { Global, Injectable } from '@nestjs/common'; import os from 'os'; import { join } from 'node:path'; import { @@ -25,6 +25,7 @@ const readFileAsync = promisify(read); const openAsync = promisify(open); const closeAsync = promisify(close); const writeAsync = promisify(write); + @Injectable() export class FileManagerService { private configFile = '.cortexrc'; @@ -35,6 +36,7 @@ export class FileManagerService { private benchmarkFoldername = 'benchmark'; private cortexEnginesFolderName = 'engines'; private cortexTelemetryFolderName = 'telemetry'; + private configProfile = 'default'; /** * Get cortex configs @@ -53,7 +55,8 @@ export class FileManagerService { try { const content = await promises.readFile(configPath, 'utf8'); - const config = yaml.load(content) as Config; + const configs = yaml.load(content) as Record; + const config = configs?.[this.configProfile] ?? {}; return { ...this.defaultConfig(), ...config, @@ -72,7 +75,12 @@ export class FileManagerService { const configPath = join(homeDir, this.configFile); // write config to file as yaml - const configString = yaml.dump(config); + const content = await promises.readFile(configPath, 'utf8'); + const currentConfig = yaml.load(content) as Record; + const configString = yaml.dump({ + ...currentConfig, + [this.configProfile]: config, + }); await promises.writeFile(configPath, configString, 'utf8'); } @@ -336,14 +344,18 @@ export class FileManagerService { const homeDir = os.homedir(); const configPath = join(homeDir, this.configFile); let config = this.defaultConfig(); - try { const content = readFileSync(configPath, 'utf8'); - config = (yaml.load(content) as Config) ?? this.defaultConfig(); + const configs = (yaml.load(content) as Record) ?? {}; + config = configs?.[this.configProfile] ?? config; } catch {} return { host: config.apiServerHost ?? 'localhost', port: config.apiServerPort ?? 1337, }; } + + public setConfigProfile(profile: string) { + this.configProfile = profile; + } } From cd8ee5f0a8a4b4e0db2978125bea7563eacd2c8e Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Fri, 2 Aug 2024 14:30:45 +0700 Subject: [PATCH 2/9] remove log --- cortex-js/src/infrastructure/commanders/base.command.ts | 1 - .../src/infrastructure/commanders/cortex-command.commander.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/cortex-js/src/infrastructure/commanders/base.command.ts b/cortex-js/src/infrastructure/commanders/base.command.ts index 3625df644..c73996cc7 100644 --- a/cortex-js/src/infrastructure/commanders/base.command.ts +++ b/cortex-js/src/infrastructure/commanders/base.command.ts @@ -34,7 +34,6 @@ export abstract class BaseCommand extends CommandRunner { process.exit(1); } checkingSpinner.succeed('API server is online'); - console.log(options, passedParam); this.cortex = new CortexClient(this.fileManagerService); await this.runCommand(passedParam, options); } diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index c61596188..d4695c1dd 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -186,7 +186,6 @@ export class CortexCommand extends CommandRunner { description: 'Name of the process', }) parseName(value: string) { - console.log(value); this.fileManagerService.setConfigProfile(value); return value; } From 0b14c914d76b61e8c46775a7751370f94e55eadc Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Fri, 2 Aug 2024 14:31:26 +0700 Subject: [PATCH 3/9] remove unused package --- cortex-js/src/infrastructure/commanders/base.command.ts | 2 -- .../services/file-manager/file-manager.service.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/cortex-js/src/infrastructure/commanders/base.command.ts b/cortex-js/src/infrastructure/commanders/base.command.ts index c73996cc7..82e997f7d 100644 --- a/cortex-js/src/infrastructure/commanders/base.command.ts +++ b/cortex-js/src/infrastructure/commanders/base.command.ts @@ -3,8 +3,6 @@ import { Injectable } from '@nestjs/common'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import ora from 'ora'; import { CortexClient } from './services/cortex.client'; -import Module from 'module'; -import { ModuleRef } from '@nestjs/core'; import { FileManagerService } from '../services/file-manager/file-manager.service'; @Injectable() 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 fc2151ea0..5f0d4a03d 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 @@ -1,5 +1,5 @@ import { Config } from '@/domain/config/config.interface'; -import { Global, Injectable } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import os from 'os'; import { join } from 'node:path'; import { From b84245ba44df3354d01edd0ea160b332a6f18363 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Fri, 2 Aug 2024 15:47:41 +0700 Subject: [PATCH 4/9] fix: create multiple CortexClient instance --- cortex-js/src/infrastructure/commanders/base.command.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cortex-js/src/infrastructure/commanders/base.command.ts b/cortex-js/src/infrastructure/commanders/base.command.ts index 82e997f7d..80d362e9d 100644 --- a/cortex-js/src/infrastructure/commanders/base.command.ts +++ b/cortex-js/src/infrastructure/commanders/base.command.ts @@ -32,7 +32,9 @@ export abstract class BaseCommand extends CommandRunner { process.exit(1); } checkingSpinner.succeed('API server is online'); - this.cortex = new CortexClient(this.fileManagerService); + if (!this.cortex) { + this.cortex = new CortexClient(this.fileManagerService); + } await this.runCommand(passedParam, options); } } From 1fe4bd26e56911f2de3b14926e4bc5a82ccc6591 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Fri, 2 Aug 2024 16:17:26 +0700 Subject: [PATCH 5/9] change fileManager service to singleton --- cortex-js/src/app.ts | 5 +-- .../infrastructure/commanders/base.command.ts | 9 ++--- .../commanders/benchmark.command.ts | 9 ++--- .../infrastructure/commanders/chat.command.ts | 11 +++--- .../commanders/cortex-command.commander.ts | 17 ++++----- .../commanders/embeddings.command.ts | 4 +- .../commanders/engines.command.ts | 4 +- .../commanders/engines/engines-get.command.ts | 4 +- .../engines/engines-init.command.ts | 7 ++-- .../engines/engines-list.command.ts | 4 +- .../commanders/engines/engines-set.command.ts | 8 +--- .../commanders/models.command.ts | 4 +- .../commanders/models/model-get.command.ts | 4 +- .../commanders/models/model-list.command.ts | 4 +- .../commanders/models/model-pull.command.ts | 11 +++--- .../commanders/models/model-remove.command.ts | 5 +-- .../commanders/models/model-start.command.ts | 13 ++++--- .../commanders/models/model-stop.command.ts | 4 +- .../commanders/models/model-update.command.ts | 4 +- .../commanders/presets.command.ts | 7 ++-- .../infrastructure/commanders/ps.command.ts | 8 ++-- .../infrastructure/commanders/run.command.ts | 13 ++++--- .../commanders/serve-stop.command.ts | 7 +--- .../commanders/services/cortex.client.ts | 4 +- .../commanders/telemetry.command.ts | 4 +- .../database/sqlite-database.providers.ts | 6 +-- .../providers/cortex/cortex.provider.ts | 11 ++---- .../extensions/extension.repository.ts | 38 ++++++------------- .../repositories/models/model.repository.ts | 16 ++++---- .../telemetry/telemetry.repository.ts | 28 ++++++-------- .../file-manager/file-manager.service.ts | 2 + cortex-js/src/usecases/chat/chat.usecases.ts | 5 +-- .../src/usecases/configs/configs.usecase.ts | 17 ++++----- .../src/usecases/cortex/cortex.usecases.ts | 29 +++++++------- .../src/usecases/engines/engines.usecase.ts | 11 +++--- .../src/usecases/models/models.usecases.ts | 13 +++---- .../usecases/telemetry/telemetry.module.ts | 3 +- 37 files changed, 144 insertions(+), 209 deletions(-) diff --git a/cortex-js/src/app.ts b/cortex-js/src/app.ts index c8c81cee4..f32e8bc3e 100644 --- a/cortex-js/src/app.ts +++ b/cortex-js/src/app.ts @@ -1,7 +1,7 @@ import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { AppModule } from './app.module'; -import { FileManagerService } from './infrastructure/services/file-manager/file-manager.service'; +import { fileManagerService } from './infrastructure/services/file-manager/file-manager.service'; import { ValidationPipe } from '@nestjs/common'; import { TelemetryUsecases } from './usecases/telemetry/telemetry.usecases'; export const getApp = async () => { @@ -14,8 +14,7 @@ export const getApp = async () => { // Set the global prefix for the API /v1/ app.setGlobalPrefix('v1'); - const fileService = app.get(FileManagerService); - await fileService.getConfig(); + await fileManagerService.getConfig(); const telemetryService = await app.resolve(TelemetryUsecases); await telemetryService.initInterval(); diff --git a/cortex-js/src/infrastructure/commanders/base.command.ts b/cortex-js/src/infrastructure/commanders/base.command.ts index 80d362e9d..e0839fcb2 100644 --- a/cortex-js/src/infrastructure/commanders/base.command.ts +++ b/cortex-js/src/infrastructure/commanders/base.command.ts @@ -3,15 +3,12 @@ import { Injectable } from '@nestjs/common'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import ora from 'ora'; import { CortexClient } from './services/cortex.client'; -import { FileManagerService } from '../services/file-manager/file-manager.service'; +import { fileManagerService } from '../services/file-manager/file-manager.service'; @Injectable() export abstract class BaseCommand extends CommandRunner { cortex: CortexClient; - constructor( - readonly cortexUseCases: CortexUsecases, - readonly fileManagerService: FileManagerService, - ) { + constructor(readonly cortexUseCases: CortexUsecases) { super(); } protected abstract runCommand( @@ -33,7 +30,7 @@ export abstract class BaseCommand extends CommandRunner { } checkingSpinner.succeed('API server is online'); if (!this.cortex) { - this.cortex = new CortexClient(this.fileManagerService); + this.cortex = new CortexClient(); } await this.runCommand(passedParam, options); } diff --git a/cortex-js/src/infrastructure/commanders/benchmark.command.ts b/cortex-js/src/infrastructure/commanders/benchmark.command.ts index 8c005a3f7..2820ded7e 100644 --- a/cortex-js/src/infrastructure/commanders/benchmark.command.ts +++ b/cortex-js/src/infrastructure/commanders/benchmark.command.ts @@ -6,7 +6,6 @@ import { import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { BaseCommand } from './base.command'; import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'; -import { FileManagerService } from '../services/file-manager/file-manager.service'; import { join } from 'path'; import yaml from 'js-yaml'; import si from 'systeminformation'; @@ -16,6 +15,7 @@ import { BenchmarkHardware } from '@/domain/telemetry/telemetry.interface'; import { defaultBenchmarkConfiguration } from '../constants/benchmark'; import { inspect } from 'util'; import { Cortex } from '@cortexso/cortex.js'; +import { fileManagerService } from '../services/file-manager/file-manager.service'; @SubCommand({ name: 'benchmark', @@ -29,10 +29,9 @@ import { Cortex } from '@cortexso/cortex.js'; export class BenchmarkCommand extends BaseCommand { constructor( readonly cortexUsecases: CortexUsecases, - private readonly fileService: FileManagerService, private readonly telemetryUsecases: TelemetryUsecases, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); } async runCommand( @@ -106,7 +105,7 @@ export class BenchmarkCommand extends BaseCommand { * @returns the benchmark configuration */ private async getBenchmarkConfig() { - const benchmarkFolder = await this.fileService.getBenchmarkPath(); + const benchmarkFolder = await fileManagerService.getBenchmarkPath(); const configurationPath = join(benchmarkFolder, 'config.yaml'); if (existsSync(configurationPath)) { return yaml.load( @@ -299,7 +298,7 @@ export class BenchmarkCommand extends BaseCommand { bar.stop(); const outputFilePath = join( - await this.fileService.getBenchmarkPath(), + await fileManagerService.getBenchmarkPath(), 'output.json', ); await this.telemetryUsecases.sendBenchmarkEvent({ diff --git a/cortex-js/src/infrastructure/commanders/chat.command.ts b/cortex-js/src/infrastructure/commanders/chat.command.ts index 61335104d..11a122f22 100644 --- a/cortex-js/src/infrastructure/commanders/chat.command.ts +++ b/cortex-js/src/infrastructure/commanders/chat.command.ts @@ -12,7 +12,7 @@ import { BaseCommand } from './base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { Engines } from './types/engine.interface'; import { join } from 'path'; -import { FileManagerService } from '../services/file-manager/file-manager.service'; +import { fileManagerService } from '../services/file-manager/file-manager.service'; import { isRemoteEngine } from '@/utils/normalize-model-id'; import { Cortex } from '@cortexso/cortex.js'; import { ChatClient } from './services/chat-client'; @@ -43,11 +43,10 @@ export class ChatCommand extends BaseCommand { constructor( private readonly inquirerService: InquirerService, private readonly telemetryUsecases: TelemetryUsecases, - private readonly fileService: FileManagerService, protected readonly cortexUsecases: CortexUsecases, protected readonly contextService: ContextService, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); this.chatClient = new ChatClient(this.cortex); } @@ -87,7 +86,9 @@ export class ChatCommand extends BaseCommand { // Pull engine if not exist if ( !isRemoteEngine(engine) && - !existsSync(join(await this.fileService.getCortexCppEnginePath(), engine)) + !existsSync( + join(await fileManagerService.getCortexCppEnginePath(), engine), + ) ) { console.log('Downloading engine...'); await this.cortex.engines.init(engine); @@ -105,7 +106,7 @@ export class ChatCommand extends BaseCommand { TelemetrySource.CLI, ); - const preset = await this.fileService.getPreset(options.preset); + const preset = await fileManagerService.getPreset(options.preset); return this.chatClient.chat( modelId, diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index d4695c1dd..67def5a06 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -15,7 +15,7 @@ import { ContextService } from '../services/context/context.service'; import { EnginesCommand } from './engines.command'; import { defaultCortexJsHost, defaultCortexJsPort } from '../constants/cortex'; import { getApp } from '@/app'; -import { FileManagerService } from '../services/file-manager/file-manager.service'; +import { fileManagerService } from '../services/file-manager/file-manager.service'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { ServeStopCommand } from './serve-stop.command'; import ora from 'ora'; @@ -54,7 +54,6 @@ export class CortexCommand extends CommandRunner { configPort: number; constructor( readonly contextService: ContextService, - readonly fileManagerService: FileManagerService, readonly cortexUseCases: CortexUsecases, ) { super(); @@ -64,7 +63,7 @@ export class CortexCommand extends CommandRunner { const { apiServerHost: configApiServerHost, apiServerPort: configApiServerPort, - } = await this.fileManagerService.getConfig(); + } = await fileManagerService.getConfig(); this.configHost = configApiServerHost || defaultCortexJsHost; this.configPort = configApiServerPort || defaultCortexJsPort; @@ -85,7 +84,7 @@ export class CortexCommand extends CommandRunner { } private async startServer(attach: boolean, dataFolderPath?: string) { - const config = await this.fileManagerService.getConfig(); + const config = await fileManagerService.getConfig(); try { const startEngineSpinner = ora('Starting Cortex engine...'); await this.cortexUseCases.startCortex().catch((e) => { @@ -103,12 +102,12 @@ export class CortexCommand extends CommandRunner { process.exit(0); } if (dataFolderPath) { - await this.fileManagerService.writeConfigFile({ + await fileManagerService.writeConfigFile({ ...config, dataFolderPath, }); // load config again to create the data folder - await this.fileManagerService.getConfig(dataFolderPath); + await fileManagerService.getConfig(dataFolderPath); } if (attach) { const app = await getApp(); @@ -124,7 +123,7 @@ export class CortexCommand extends CommandRunner { `API Playground available at http://${this.host}:${this.port}/api`, ), ); - await this.fileManagerService.writeConfigFile({ + await fileManagerService.writeConfigFile({ ...config, apiServerHost: this.host, apiServerPort: this.port, @@ -134,7 +133,7 @@ export class CortexCommand extends CommandRunner { } catch (e) { console.error(e); // revert the data folder path if it was set - await this.fileManagerService.writeConfigFile({ + await fileManagerService.writeConfigFile({ ...config, }); console.error(`Failed to start server. Is port ${this.port} in use?`); @@ -186,7 +185,7 @@ export class CortexCommand extends CommandRunner { description: 'Name of the process', }) parseName(value: string) { - this.fileManagerService.setConfigProfile(value); + fileManagerService.setConfigProfile(value); return value; } } diff --git a/cortex-js/src/infrastructure/commanders/embeddings.command.ts b/cortex-js/src/infrastructure/commanders/embeddings.command.ts index 941920c7e..bf2dc8df3 100644 --- a/cortex-js/src/infrastructure/commanders/embeddings.command.ts +++ b/cortex-js/src/infrastructure/commanders/embeddings.command.ts @@ -4,7 +4,6 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { BaseCommand } from './base.command'; import { Cortex } from '@cortexso/cortex.js'; import ora from 'ora'; -import { FileManagerService } from '../services/file-manager/file-manager.service'; interface EmbeddingCommandOptions { encoding_format?: string; @@ -25,9 +24,8 @@ export class EmbeddingCommand extends BaseCommand { constructor( private readonly inquirerService: InquirerService, readonly cortexUsecases: CortexUsecases, - private readonly fileService: FileManagerService, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); } async runCommand( passedParams: string[], diff --git a/cortex-js/src/infrastructure/commanders/engines.command.ts b/cortex-js/src/infrastructure/commanders/engines.command.ts index da88c3928..9f3e84b82 100644 --- a/cortex-js/src/infrastructure/commanders/engines.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines.command.ts @@ -10,7 +10,6 @@ import { EngineNamesMap } from './types/engine.interface'; import { BaseCommand } from './base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { EnginesSetCommand } from './engines/engines-set.command'; -import { FileManagerService } from '../services/file-manager/file-manager.service'; @SubCommand({ name: 'engines', @@ -31,9 +30,8 @@ export class EnginesCommand extends BaseCommand { readonly contextService: ContextService, readonly moduleRef: ModuleRef, readonly cortexUsecases: CortexUsecases, - readonly fileService: FileManagerService, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); } async runCommand(passedParams: string[], options: { vulkan: boolean }) { const [parameter, command] = passedParams; 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 01db3e8ec..912087a6d 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,6 @@ 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 { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: ' get', @@ -18,9 +17,8 @@ export class EnginesGetCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, - readonly fileService: FileManagerService, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); } async runCommand(passedParams: string[]): Promise { 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 3ae5c4627..c65075e5d 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts @@ -3,12 +3,12 @@ import { SetCommandContext } from '../decorators/CommandContext'; import { ContextService } from '@/infrastructure/services/context/context.service'; import { Engines } from '../types/engine.interface'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { BaseCommand } from '../base.command'; import { defaultInstallationOptions } from '@/utils/init'; import { Presets, SingleBar } from 'cli-progress'; import ora from 'ora'; import { InitEngineDto } from '@/infrastructure/dtos/engines/engines.dto'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: ' init', @@ -21,10 +21,9 @@ import { InitEngineDto } from '@/infrastructure/dtos/engines/engines.dto'; export class EnginesInitCommand extends BaseCommand { constructor( private readonly cortexUsecases: CortexUsecases, - readonly fileManagerService: FileManagerService, readonly contextService: ContextService, ) { - super(cortexUsecases, fileManagerService); + super(cortexUsecases); } async runCommand( @@ -39,7 +38,7 @@ export class EnginesInitCommand extends BaseCommand { } : {}; - const configs = await this.fileManagerService.getConfig(); + const configs = await fileManagerService.getConfig(); const host = configs.cortexCppHost; const port = configs.cortexCppPort; // Should stop cortex before installing engine 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 b565030ac..25f85949f 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts @@ -4,7 +4,6 @@ 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 { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: 'list', @@ -15,9 +14,8 @@ export class EnginesListCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - readonly fileService: FileManagerService, ) { - super(cortexUseCases, fileService); + super(cortexUseCases); } async runCommand(): Promise { 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 d4c1d70e7..655c7d614 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,6 @@ import { SubCommand } from 'nest-commander'; import { SetCommandContext } from '../decorators/CommandContext'; import { BaseCommand } from '../base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: ' set ', @@ -13,11 +12,8 @@ import { FileManagerService } from '@/infrastructure/services/file-manager/file- }) @SetCommandContext() export class EnginesSetCommand extends BaseCommand { - constructor( - readonly cortexUsecases: CortexUsecases, - readonly fileService: FileManagerService, - ) { - super(cortexUsecases, fileService); + constructor(readonly cortexUsecases: CortexUsecases) { + super(cortexUsecases); } async runCommand(passedParams: string[]): Promise { diff --git a/cortex-js/src/infrastructure/commanders/models.command.ts b/cortex-js/src/infrastructure/commanders/models.command.ts index 777a63c44..ab3f562b4 100644 --- a/cortex-js/src/infrastructure/commanders/models.command.ts +++ b/cortex-js/src/infrastructure/commanders/models.command.ts @@ -9,7 +9,6 @@ import { BaseCommand } from './base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { ModuleRef } from '@nestjs/core'; import { ModelPullCommand } from './models/model-pull.command'; -import { FileManagerService } from '../services/file-manager/file-manager.service'; @SubCommand({ name: 'models', @@ -27,9 +26,8 @@ export class ModelsCommand extends BaseCommand { constructor( private readonly moduleRef: ModuleRef, readonly cortexUsecases: CortexUsecases, - readonly fileService: FileManagerService, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); } commandMap: { [key: string]: any } = { 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 d6661c3df..3bc4e8fa5 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-get.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-get.command.ts @@ -3,7 +3,6 @@ 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 { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: 'get', @@ -18,9 +17,8 @@ export class ModelGetCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - readonly fileService: FileManagerService, ) { - super(cortexUseCases, fileService); + super(cortexUseCases); } async runCommand(passedParams: string[]): Promise { 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 7acab8eda..9e169733a 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-list.command.ts @@ -3,7 +3,6 @@ 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 { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; interface ModelListOptions { format: 'table' | 'json'; @@ -14,9 +13,8 @@ export class ModelListCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - readonly fileService: FileManagerService, ) { - super(cortexUseCases, fileService); + super(cortexUseCases); } async runCommand( 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 6bc99861c..179484845 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts @@ -1,4 +1,4 @@ -import { exit, stdin, stdout } from 'node:process'; +import { exit } from 'node:process'; import { SubCommand } from 'nest-commander'; import { SetCommandContext } from '../decorators/CommandContext'; import { ModelNotFoundException } from '@/infrastructure/exception/model-not-found.exception'; @@ -10,7 +10,6 @@ import { import { ContextService } from '@/infrastructure/services/context/context.service'; import { existsSync } from 'fs'; import { join } from 'node:path'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { checkModelCompatibility } from '@/utils/model-check'; import { Engines } from '../types/engine.interface'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; @@ -19,6 +18,7 @@ import { downloadProgress } from '@/utils/download-progress'; import { DownloadType } from '@/domain/models/download.interface'; import ora from 'ora'; import { isRemoteEngine } from '@/utils/normalize-model-id'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: 'pull', @@ -31,12 +31,11 @@ import { isRemoteEngine } from '@/utils/normalize-model-id'; @SetCommandContext() export class ModelPullCommand extends BaseCommand { constructor( - private readonly fileService: FileManagerService, private readonly telemetryUsecases: TelemetryUsecases, readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); } async runCommand(passedParams: string[]) { @@ -69,7 +68,9 @@ export class ModelPullCommand extends BaseCommand { // Pull engine if not exist if ( !isRemoteEngine(engine) && - !existsSync(join(await this.fileService.getCortexCppEnginePath(), engine)) + !existsSync( + join(await fileManagerService.getCortexCppEnginePath(), engine), + ) ) { console.log('\n'); console.log('Downloading engine...'); 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 2ff010107..52e8aefdc 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts @@ -1,10 +1,8 @@ import { SubCommand } from 'nest-commander'; -import { exit } from 'node:process'; 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 { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: 'remove', @@ -19,9 +17,8 @@ export class ModelRemoveCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - readonly fileService: FileManagerService, ) { - super(cortexUseCases, fileService); + super(cortexUseCases); } async runCommand(passedParams: string[]): Promise { 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 5c61a7eec..f345287c6 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-start.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-start.command.ts @@ -5,7 +5,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { SetCommandContext } from '../decorators/CommandContext'; import { ContextService } from '@/infrastructure/services/context/context.service'; import { existsSync } from 'node:fs'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { join } from 'node:path'; import { Engines } from '../types/engine.interface'; import { checkModelCompatibility } from '@/utils/model-check'; @@ -33,10 +33,9 @@ export class ModelStartCommand extends BaseCommand { constructor( private readonly inquirerService: InquirerService, readonly cortexUsecases: CortexUsecases, - private readonly fileService: FileManagerService, readonly contextService: ContextService, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); } async runCommand( @@ -69,14 +68,16 @@ export class ModelStartCommand extends BaseCommand { // Pull engine if not exist if ( !isRemoteEngine(engine) && - !existsSync(join(await this.fileService.getCortexCppEnginePath(), engine)) + !existsSync( + join(await fileManagerService.getCortexCppEnginePath(), engine), + ) ) { console.log('Downloading engine...'); await this.cortex.engines.init(engine); await downloadProgress(this.cortex, undefined, DownloadType.Engine); } - const parsedPreset = await this.fileService.getPreset(options.preset); + const parsedPreset = await fileManagerService.getPreset(options.preset); const startingSpinner = ora('Loading model...').start(); @@ -85,7 +86,7 @@ export class ModelStartCommand extends BaseCommand { .then(() => startingSpinner.succeed('Model loaded')) .catch(async (error) => { startingSpinner.fail(error.message ?? error); - printLastErrorLines(await this.fileService.getLogPath()); + printLastErrorLines(await fileManagerService.getLogPath()); }); } 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 aa4194f98..2eb65666a 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts @@ -4,7 +4,6 @@ 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 { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ name: 'stop', @@ -19,9 +18,8 @@ export class ModelStopCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - readonly fileService: FileManagerService, ) { - super(cortexUseCases, fileService); + super(cortexUseCases); } async runCommand(passedParams: string[]): Promise { 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 7068ca42e..a9d5f84ed 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,6 @@ 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 { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; type UpdateOptions = { model?: string; @@ -25,9 +24,8 @@ export class ModelUpdateCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - readonly fileService: FileManagerService, ) { - super(cortexUseCases, fileService); + super(cortexUseCases); } async runCommand( diff --git a/cortex-js/src/infrastructure/commanders/presets.command.ts b/cortex-js/src/infrastructure/commanders/presets.command.ts index b365d5f49..ba1ea5057 100644 --- a/cortex-js/src/infrastructure/commanders/presets.command.ts +++ b/cortex-js/src/infrastructure/commanders/presets.command.ts @@ -1,4 +1,3 @@ -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { readdirSync } from 'fs'; import { SubCommand } from 'nest-commander'; import { join } from 'path'; @@ -6,6 +5,7 @@ import { SetCommandContext } from './decorators/CommandContext'; import { ContextService } from '../services/context/context.service'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { BaseCommand } from './base.command'; +import { fileManagerService } from '../services/file-manager/file-manager.service'; @SubCommand({ name: 'presets', @@ -14,16 +14,15 @@ import { BaseCommand } from './base.command'; @SetCommandContext() export class PresetCommand extends BaseCommand { constructor( - private readonly fileService: FileManagerService, readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); } async runCommand(): Promise { return console.table( readdirSync( - join(await this.fileService.getDataFolderPath(), `presets`), + join(await fileManagerService.getDataFolderPath(), `presets`), ).map((e) => ({ preset: e.replace('.yaml', ''), })), diff --git a/cortex-js/src/infrastructure/commanders/ps.command.ts b/cortex-js/src/infrastructure/commanders/ps.command.ts index 1dfa22add..b4c2d30b0 100644 --- a/cortex-js/src/infrastructure/commanders/ps.command.ts +++ b/cortex-js/src/infrastructure/commanders/ps.command.ts @@ -2,7 +2,6 @@ import ora from 'ora'; import systeminformation from 'systeminformation'; import { SubCommand } from 'nest-commander'; import { SetCommandContext } from './decorators/CommandContext'; -import { ContextService } from '../services/context/context.service'; import { ModelStat } from './types/model-stat.interface'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { BaseCommand } from './base.command'; @@ -11,8 +10,8 @@ import { Engines } from './types/engine.interface'; import { ModelStatResponse } from '../providers/cortex/cortex.provider'; import { firstValueFrom } from 'rxjs'; import { HttpService } from '@nestjs/axios'; -import { FileManagerService } from '../services/file-manager/file-manager.service'; import { CORTEX_CPP_MODELS_URL } from '../constants/cortex'; +import { fileManagerService } from '../services/file-manager/file-manager.service'; @SubCommand({ name: 'ps', @@ -23,9 +22,8 @@ export class PSCommand extends BaseCommand { constructor( readonly cortexUsecases: CortexUsecases, private readonly httpService: HttpService, - private readonly fileService: FileManagerService, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); } async runCommand(): Promise { const runningSpinner = ora('Running PS command...').start(); @@ -77,7 +75,7 @@ export class PSCommand extends BaseCommand { * Get models running in the Cortex C++ server */ async getModels(): Promise { - const configs = await this.fileService.getConfig(); + const configs = await fileManagerService.getConfig(); const runningSpinner = ora('Getting models...').start(); return new Promise((resolve, reject) => firstValueFrom( diff --git a/cortex-js/src/infrastructure/commanders/run.command.ts b/cortex-js/src/infrastructure/commanders/run.command.ts index 30a72ccdf..f5a9393d1 100644 --- a/cortex-js/src/infrastructure/commanders/run.command.ts +++ b/cortex-js/src/infrastructure/commanders/run.command.ts @@ -4,7 +4,6 @@ import { exit } from 'node:process'; import ora from 'ora'; import { existsSync } from 'fs'; import { join } from 'path'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { Engines } from './types/engine.interface'; import { checkModelCompatibility } from '@/utils/model-check'; import { BaseCommand } from './base.command'; @@ -15,6 +14,7 @@ import { DownloadType } from '@/domain/models/download.interface'; import { isLocalFile } from '@/utils/urls'; import { parse } from 'node:path'; import { printLastErrorLines } from '@/utils/logs'; +import { fileManagerService } from '../services/file-manager/file-manager.service'; type RunOptions = { threadId?: string; @@ -36,9 +36,8 @@ export class RunCommand extends BaseCommand { constructor( protected readonly cortexUsecases: CortexUsecases, private readonly inquirerService: InquirerService, - private readonly fileService: FileManagerService, ) { - super(cortexUsecases, fileService); + super(cortexUsecases); this.chatClient = new ChatClient(this.cortex); } @@ -92,7 +91,9 @@ export class RunCommand extends BaseCommand { // Pull engine if not exist if ( !isRemoteEngine(engine) && - !existsSync(join(await this.fileService.getCortexCppEnginePath(), engine)) + !existsSync( + join(await fileManagerService.getCortexCppEnginePath(), engine), + ) ) { console.log('Downloading engine...'); await this.cortex.engines.init(engine); @@ -102,7 +103,7 @@ export class RunCommand extends BaseCommand { const startingSpinner = ora('Loading model...').start(); return this.cortex.models - .start(modelId, await this.fileService.getPreset(options.preset)) + .start(modelId, await fileManagerService.getPreset(options.preset)) .then(() => { startingSpinner.succeed('Model loaded'); if (options.chat) this.chatClient.chat(modelId, options.threadId); @@ -111,7 +112,7 @@ export class RunCommand extends BaseCommand { .catch(async (e) => { startingSpinner.fail(e.message ?? e); - printLastErrorLines(await this.fileService.getLogPath()); + printLastErrorLines(await fileManagerService.getLogPath()); }); } diff --git a/cortex-js/src/infrastructure/commanders/serve-stop.command.ts b/cortex-js/src/infrastructure/commanders/serve-stop.command.ts index 4f2c7e0f1..3fa96be9e 100644 --- a/cortex-js/src/infrastructure/commanders/serve-stop.command.ts +++ b/cortex-js/src/infrastructure/commanders/serve-stop.command.ts @@ -8,11 +8,8 @@ import { FileManagerService } from '../services/file-manager/file-manager.servic description: 'Stop the API server', }) export class ServeStopCommand extends BaseCommand { - constructor( - private readonly cortexUsecases: CortexUsecases, - readonly fileService: FileManagerService, - ) { - super(cortexUsecases, fileService); + constructor(private readonly cortexUsecases: CortexUsecases) { + super(cortexUsecases); } async runCommand(): Promise { return this.cortexUsecases diff --git a/cortex-js/src/infrastructure/commanders/services/cortex.client.ts b/cortex-js/src/infrastructure/commanders/services/cortex.client.ts index fa7ae4593..ce921a1f0 100644 --- a/cortex-js/src/infrastructure/commanders/services/cortex.client.ts +++ b/cortex-js/src/infrastructure/commanders/services/cortex.client.ts @@ -2,13 +2,13 @@ import { cortexNamespace, cortexServerAPI, } from '@/infrastructure/constants/cortex'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +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(fileManagerService: FileManagerService) { + constructor() { const configs = fileManagerService.getServerConfig(); super({ baseURL: cortexServerAPI(configs.host, configs.port), diff --git a/cortex-js/src/infrastructure/commanders/telemetry.command.ts b/cortex-js/src/infrastructure/commanders/telemetry.command.ts index 7b966c895..b4f3b3ec5 100644 --- a/cortex-js/src/infrastructure/commanders/telemetry.command.ts +++ b/cortex-js/src/infrastructure/commanders/telemetry.command.ts @@ -4,7 +4,6 @@ import { TelemetryOptions } from './types/telemetry-options.interface'; import { SetCommandContext } from './decorators/CommandContext'; import { BaseCommand } from './base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; -import { FileManagerService } from '../services/file-manager/file-manager.service'; @SubCommand({ name: 'telemetry', @@ -15,9 +14,8 @@ export class TelemetryCommand extends BaseCommand { constructor( private readonly telemetryUseCase: TelemetryUsecases, readonly cortexUseCases: CortexUsecases, - readonly fileService: FileManagerService, ) { - super(cortexUseCases, fileService); + super(cortexUseCases); } async runCommand( diff --git a/cortex-js/src/infrastructure/database/sqlite-database.providers.ts b/cortex-js/src/infrastructure/database/sqlite-database.providers.ts index a37eea421..8956abea0 100644 --- a/cortex-js/src/infrastructure/database/sqlite-database.providers.ts +++ b/cortex-js/src/infrastructure/database/sqlite-database.providers.ts @@ -1,16 +1,16 @@ -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { databaseFile } from '@/infrastructure/constants/cortex'; import { join } from 'path'; import { ThreadEntity } from '../entities/thread.entity'; import { MessageEntity } from '../entities/message.entity'; import { AssistantEntity } from '../entities/assistant.entity'; import { Sequelize } from 'sequelize-typescript'; +import { fileManagerService } from '../services/file-manager/file-manager.service'; export const sqliteDatabaseProviders = [ { provide: 'DATA_SOURCE', - inject: [FileManagerService], - useFactory: async (fileManagerService: FileManagerService) => { + inject: [], + useFactory: async () => { const dataFolderPath = await fileManagerService.getDataFolderPath(); const sqlitePath = join(dataFolderPath, databaseFile); const sequelize = new Sequelize({ diff --git a/cortex-js/src/infrastructure/providers/cortex/cortex.provider.ts b/cortex-js/src/infrastructure/providers/cortex/cortex.provider.ts index ad4664acf..8663da7ac 100644 --- a/cortex-js/src/infrastructure/providers/cortex/cortex.provider.ts +++ b/cortex-js/src/infrastructure/providers/cortex/cortex.provider.ts @@ -12,7 +12,7 @@ import { import { readdirSync } from 'node:fs'; import { normalizeModelId } from '@/utils/normalize-model-id'; import { firstValueFrom } from 'rxjs'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; export interface ModelStatResponse { object: string; @@ -32,10 +32,7 @@ export default class CortexProvider extends OAIEngineExtension { private loadModelUrl = `http://${defaultCortexCppHost}:${defaultCortexCppPort}/inferences/server/loadmodel`; private unloadModelUrl = `http://${defaultCortexCppHost}:${defaultCortexCppPort}/inferences/server/unloadmodel`; - constructor( - protected readonly httpService: HttpService, - protected readonly fileManagerService: FileManagerService, - ) { + constructor(protected readonly httpService: HttpService) { super(httpService); } @@ -44,7 +41,7 @@ export default class CortexProvider extends OAIEngineExtension { model: Model, settings?: ModelSettingParams, ): Promise { - const modelsContainerDir = await this.fileManagerService.getModelsPath(); + const modelsContainerDir = await fileManagerService.getModelsPath(); let llama_model_path = settings?.llama_model_path; if (!llama_model_path) { @@ -101,7 +98,7 @@ export default class CortexProvider extends OAIEngineExtension { // Override the isModelRunning method to check if the model is running override async isModelRunning(modelId: string): Promise { - const configs = await this.fileManagerService.getConfig(); + const configs = await fileManagerService.getConfig(); return firstValueFrom( this.httpService.get( diff --git a/cortex-js/src/infrastructure/repositories/extensions/extension.repository.ts b/cortex-js/src/infrastructure/repositories/extensions/extension.repository.ts index 464efcc44..a0a22398b 100644 --- a/cortex-js/src/infrastructure/repositories/extensions/extension.repository.ts +++ b/cortex-js/src/infrastructure/repositories/extensions/extension.repository.ts @@ -3,7 +3,10 @@ import { ExtensionRepository } from '@/domain/repositories/extension.interface'; import { Extension } from '@/domain/abstracts/extension.abstract'; import { readdir, lstat } from 'fs/promises'; import { join } from 'path'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +import { + fileManagerService, + FileManagerService, +} from '@/infrastructure/services/file-manager/file-manager.service'; import { existsSync, mkdirSync, watch } from 'fs'; import { Engines } from '@/infrastructure/commanders/types/engine.interface'; import { OAIEngineExtension } from '@/domain/abstracts/oai.abstract'; @@ -19,17 +22,15 @@ export class ExtensionRepositoryImpl implements ExtensionRepository { extensions = new Map(); constructor( - private readonly fileService: FileManagerService, @Inject('EXTENSIONS_PROVIDER') private readonly coreExtensions: OAIEngineExtension[], private readonly httpService: HttpService, - private readonly fileManagerService: FileManagerService, ) { this.loadCoreExtensions(); this.loadExternalExtensions(); // Watch engine folder only for now - fileService.getCortexCppEnginePath().then((path) => { + fileManagerService.getCortexCppEnginePath().then((path) => { if (!existsSync(path)) mkdirSync(path); watch(path, (eventType, filename) => { this.extensions.clear(); @@ -84,43 +85,28 @@ export class ExtensionRepositoryImpl implements ExtensionRepository { } private async loadCoreExtensions() { - const llamaCPPEngine = new LlamaCPPProvider( - this.httpService, - this.fileManagerService, - ); + const llamaCPPEngine = new LlamaCPPProvider(this.httpService); llamaCPPEngine.status = existsSync( - join( - await this.fileManagerService.getCortexCppEnginePath(), - Engines.llamaCPP, - ), + join(await fileManagerService.getCortexCppEnginePath(), Engines.llamaCPP), ) ? EngineStatus.READY : EngineStatus.NOT_INITIALIZED; - const onnxEngine = new Onnxprovider( - this.httpService, - this.fileManagerService, - ); + const onnxEngine = new Onnxprovider(this.httpService); onnxEngine.status = existsSync( - join( - await this.fileManagerService.getCortexCppEnginePath(), - Engines.onnx, - ), + join(await fileManagerService.getCortexCppEnginePath(), Engines.onnx), ) && process.platform === 'win32' ? EngineStatus.READY : process.platform !== 'win32' ? EngineStatus.NOT_SUPPORTED : EngineStatus.NOT_INITIALIZED; - const tensorrtLLMEngine = new TensorrtLLMProvider( - this.httpService, - this.fileManagerService, - ); + const tensorrtLLMEngine = new TensorrtLLMProvider(this.httpService); tensorrtLLMEngine.status = existsSync( join( - await this.fileManagerService.getCortexCppEnginePath(), + await fileManagerService.getCortexCppEnginePath(), Engines.tensorrtLLM, ), ) && process.platform !== 'darwin' @@ -146,7 +132,7 @@ export class ExtensionRepositoryImpl implements ExtensionRepository { private async loadExternalExtensions() { const extensionsPath = process.env.EXTENSIONS_PATH ?? - (await this.fileService.getExtensionsPath()); + (await fileManagerService.getExtensionsPath()); this.loadExtensions(extensionsPath); } diff --git a/cortex-js/src/infrastructure/repositories/models/model.repository.ts b/cortex-js/src/infrastructure/repositories/models/model.repository.ts index bd5e04de6..c2625d3c9 100644 --- a/cortex-js/src/infrastructure/repositories/models/model.repository.ts +++ b/cortex-js/src/infrastructure/repositories/models/model.repository.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import { join, extname, basename } from 'path'; import { ModelRepository } from '@/domain/repositories/model.interface'; import { Model } from '@/domain/models/model.interface'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { existsSync, mkdirSync, @@ -24,9 +24,9 @@ export class ModelRepositoryImpl implements ModelRepository { // Check whether the models have been loaded or not. loaded = false; - constructor(private readonly fileService: FileManagerService) { + constructor() { this.loadModels(); - fileService.getModelsPath().then((path) => { + fileManagerService.getModelsPath().then((path) => { if (!existsSync(path)) mkdirSync(path); watch(path, (eventType, filename) => { this.loadModels(true); @@ -42,13 +42,13 @@ export class ModelRepositoryImpl implements ModelRepository { */ async create(object: Model): Promise { const modelsFolderPath = join( - await this.fileService.getDataFolderPath(), + await fileManagerService.getDataFolderPath(), 'models', ); const modelYaml = dump(object); if (!existsSync(modelsFolderPath)) mkdirSync(modelsFolderPath); const modelsPath = - process.env.EXTENSIONS_PATH ?? (await this.fileService.getModelsPath()); + process.env.EXTENSIONS_PATH ?? (await fileManagerService.getModelsPath()); writeFileSync( join(modelsPath, `${normalizeModelId(object.model)}.yaml`), modelYaml, @@ -95,7 +95,7 @@ export class ModelRepositoryImpl implements ModelRepository { const modelYaml = dump(updatedModel); const modelsPath = - process.env.EXTENSIONS_PATH ?? (await this.fileService.getModelsPath()); + process.env.EXTENSIONS_PATH ?? (await fileManagerService.getModelsPath()); writeFileSync( join( @@ -116,7 +116,7 @@ export class ModelRepositoryImpl implements ModelRepository { async remove(id: string): Promise { this.models.delete(id); const yamlFilePath = join( - await this.fileService.getModelsPath(), + await fileManagerService.getModelsPath(), this.fileModel.get(id) ?? id, ); if (existsSync(yamlFilePath)) rmSync(yamlFilePath); @@ -131,7 +131,7 @@ export class ModelRepositoryImpl implements ModelRepository { private async loadModels(forceReload: boolean = false): Promise { if (this.loaded && !forceReload) return Array.from(this.models.values()); const modelsPath = - process.env.EXTENSIONS_PATH ?? (await this.fileService.getModelsPath()); + process.env.EXTENSIONS_PATH ?? (await fileManagerService.getModelsPath()); this.models.clear(); this.fileModel.clear(); diff --git a/cortex-js/src/infrastructure/repositories/telemetry/telemetry.repository.ts b/cortex-js/src/infrastructure/repositories/telemetry/telemetry.repository.ts index 52a514d6c..d276e01a9 100644 --- a/cortex-js/src/infrastructure/repositories/telemetry/telemetry.repository.ts +++ b/cortex-js/src/infrastructure/repositories/telemetry/telemetry.repository.ts @@ -19,8 +19,8 @@ import { join } from 'path'; import packageJson from '@/../package.json'; import axios from 'axios'; import { telemetryServerUrl } from '@/infrastructure/constants/cortex'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { ModelStat } from '@/infrastructure/commanders/types/model-stat.interface'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; // refactor using convert to dto @Injectable() @@ -34,13 +34,11 @@ export class TelemetryRepositoryImpl implements TelemetryRepository { private readonly crashReportFileName = 'crash-report.jsonl'; private readonly anonymizedDataFileName = 'session.json'; - constructor(private readonly fileManagerService: FileManagerService) {} + constructor() {} private async getTelemetryDirectory(): Promise { - const dataFolderPath = await this.fileManagerService.getDataFolderPath(); - await this.fileManagerService.createFolderIfNotExistInDataFolder( - 'telemetry', - ); + const dataFolderPath = await fileManagerService.getDataFolderPath(); + await fileManagerService.createFolderIfNotExistInDataFolder('telemetry'); return join(dataFolderPath, 'telemetry'); } @@ -95,12 +93,12 @@ export class TelemetryRepositoryImpl implements TelemetryRepository { async markLastCrashReportAsSent(): Promise { try { - const { data, position } = await this.fileManagerService.getLastLine( + const { data, position } = await fileManagerService.getLastLine( join(await this.getTelemetryDirectory(), this.crashReportFileName), ); const Telemetry = JSON.parse(data) as Telemetry; Telemetry.metadata.sentAt = new Date().toISOString(); - await this.fileManagerService.modifyLine( + await fileManagerService.modifyLine( join(await this.getTelemetryDirectory(), this.crashReportFileName), JSON.stringify(Telemetry), position, @@ -109,7 +107,7 @@ export class TelemetryRepositoryImpl implements TelemetryRepository { } async readCrashReports(callback: (Telemetry: Telemetry) => void) { - this.fileManagerService.readLines( + fileManagerService.readLines( join(await this.getTelemetryDirectory(), this.crashReportFileName), (line: string) => { const data = JSON.parse(line) as Telemetry; @@ -120,10 +118,8 @@ export class TelemetryRepositoryImpl implements TelemetryRepository { async getLastCrashReport(): Promise { try { - await this.fileManagerService.createFolderIfNotExistInDataFolder( - 'telemetry', - ); - const { data } = await this.fileManagerService.getLastLine( + await fileManagerService.createFolderIfNotExistInDataFolder('telemetry'); + const { data } = await fileManagerService.getLastLine( join(await this.getTelemetryDirectory(), this.crashReportFileName), ); if (!data) { @@ -148,7 +144,7 @@ export class TelemetryRepositoryImpl implements TelemetryRepository { createdAt: new Date().toISOString(), sentAt: null, }; - return this.fileManagerService.append( + return fileManagerService.append( join(await this.getTelemetryDirectory(), this.crashReportFileName), JSON.stringify({ metadata, @@ -259,7 +255,7 @@ export class TelemetryRepositoryImpl implements TelemetryRepository { } async getAnonymizedData(): Promise { - const content = await this.fileManagerService.readFile( + const content = await fileManagerService.readFile( join(await this.getTelemetryDirectory(), this.anonymizedDataFileName), ); @@ -272,7 +268,7 @@ export class TelemetryRepositoryImpl implements TelemetryRepository { } async updateAnonymousData(data: TelemetryAnonymized): Promise { - return this.fileManagerService.writeFile( + return fileManagerService.writeFile( join(await this.getTelemetryDirectory(), this.anonymizedDataFileName), JSON.stringify(data), ); 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 5f0d4a03d..87bef9802 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 @@ -359,3 +359,5 @@ export class FileManagerService { this.configProfile = profile; } } + +export const fileManagerService = new FileManagerService(); diff --git a/cortex-js/src/usecases/chat/chat.usecases.ts b/cortex-js/src/usecases/chat/chat.usecases.ts index 1a6784f49..bf38e9cfb 100644 --- a/cortex-js/src/usecases/chat/chat.usecases.ts +++ b/cortex-js/src/usecases/chat/chat.usecases.ts @@ -12,10 +12,10 @@ import { defaultEmbeddingModel, } from '@/infrastructure/constants/cortex'; import { CreateEmbeddingsDto } from '@/infrastructure/dtos/embeddings/embeddings-request.dto'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { Engines } from '@/infrastructure/commanders/types/engine.interface'; import { ModelsUsecases } from '../models/models.usecases'; import { isRemoteEngine } from '@/utils/normalize-model-id'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @Injectable() export class ChatUsecases { @@ -24,7 +24,6 @@ export class ChatUsecases { private readonly telemetryUseCases: TelemetryUsecases, private readonly modelsUsescases: ModelsUsecases, private readonly httpService: HttpService, - private readonly fileService: FileManagerService, ) {} async inference( @@ -94,7 +93,7 @@ export class ChatUsecases { }); } - const configs = await this.fileService.getConfig(); + const configs = await fileManagerService.getConfig(); return firstValueFrom( this.httpService.post( diff --git a/cortex-js/src/usecases/configs/configs.usecase.ts b/cortex-js/src/usecases/configs/configs.usecase.ts index 4c95b4580..0332fc1ce 100644 --- a/cortex-js/src/usecases/configs/configs.usecase.ts +++ b/cortex-js/src/usecases/configs/configs.usecase.ts @@ -1,14 +1,11 @@ import { CommonResponseDto } from '@/infrastructure/dtos/common/common-response.dto'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { Injectable } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; @Injectable() export class ConfigsUsecases { - constructor( - private readonly fileManagerService: FileManagerService, - private readonly eventEmitter: EventEmitter2, - ) {} + constructor(private readonly eventEmitter: EventEmitter2) {} /** * Save a configuration to the .cortexrc file. @@ -20,7 +17,7 @@ export class ConfigsUsecases { value: string, engine?: string, ): Promise { - const configs = await this.fileManagerService.getConfig(); + const configs = await fileManagerService.getConfig(); const groupConfigs = configs[ engine as keyof typeof configs @@ -37,7 +34,7 @@ export class ConfigsUsecases { : {}), }; - return this.fileManagerService + return fileManagerService .writeConfigFile(newConfigs) .then(async () => { if (engine) { @@ -61,7 +58,7 @@ export class ConfigsUsecases { * @returns */ async getGroupConfigs(group: string) { - const configs = await this.fileManagerService.getConfig(); + const configs = await fileManagerService.getConfig(); return configs[group as keyof typeof configs] as unknown as object; } @@ -71,7 +68,7 @@ export class ConfigsUsecases { * @returns */ async getConfigs() { - return this.fileManagerService.getConfig(); + return fileManagerService.getConfig(); } /** @@ -81,7 +78,7 @@ export class ConfigsUsecases { * @returns */ async getKeyConfig(key: string) { - const configs = await this.fileManagerService.getConfig(); + const configs = await fileManagerService.getConfig(); return configs[key as keyof typeof configs]; } } diff --git a/cortex-js/src/usecases/cortex/cortex.usecases.ts b/cortex-js/src/usecases/cortex/cortex.usecases.ts index 07a4c5aa9..4850cac77 100644 --- a/cortex-js/src/usecases/cortex/cortex.usecases.ts +++ b/cortex-js/src/usecases/cortex/cortex.usecases.ts @@ -9,7 +9,7 @@ import { CortexOperationSuccessfullyDto } from '@/infrastructure/dtos/cortex/cor import { HttpService } from '@nestjs/axios'; import { firstValueFrom } from 'rxjs'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { CORTEX_CPP_HEALTH_Z_URL, CORTEX_CPP_PROCESS_DESTROY_URL, @@ -23,10 +23,7 @@ import { openSync } from 'fs'; export class CortexUsecases implements BeforeApplicationShutdown { private cortexProcess: ChildProcess | undefined; - constructor( - private readonly httpService: HttpService, - private readonly fileManagerService: FileManagerService, - ) {} + constructor(private readonly httpService: HttpService) {} /** * Start the Cortex CPP process @@ -34,7 +31,7 @@ export class CortexUsecases implements BeforeApplicationShutdown { * @returns */ async startCortex(): Promise { - const configs = await this.fileManagerService.getConfig(); + const configs = await fileManagerService.getConfig(); const host = configs.cortexCppHost; const port = configs.cortexCppPort; if (this.cortexProcess || (await this.healthCheck(host, port))) { @@ -44,10 +41,10 @@ export class CortexUsecases implements BeforeApplicationShutdown { }; } - const engineDir = await this.fileManagerService.getCortexCppEnginePath(); - const dataFolderPath = await this.fileManagerService.getDataFolderPath(); + const engineDir = await fileManagerService.getCortexCppEnginePath(); + const dataFolderPath = await fileManagerService.getDataFolderPath(); - const writer = openSync(await this.fileManagerService.getLogPath(), 'a+'); + const writer = openSync(await fileManagerService.getLogPath(), 'a+'); // Attempt to stop the process if it's already running await this.stopCortex(); @@ -93,7 +90,7 @@ export class CortexUsecases implements BeforeApplicationShutdown { .catch(reject); }, 1000); }).then((res) => { - this.fileManagerService.writeConfigFile({ + fileManagerService.writeConfigFile({ ...configs, cortexCppHost: host, cortexCppPort: port, @@ -106,7 +103,7 @@ export class CortexUsecases implements BeforeApplicationShutdown { * Stop the Cortex CPP process */ async stopCortex(): Promise { - const configs = await this.fileManagerService.getConfig(); + const configs = await fileManagerService.getConfig(); try { await firstValueFrom( this.httpService.delete( @@ -146,7 +143,7 @@ export class CortexUsecases implements BeforeApplicationShutdown { * start the API server in detached mode */ async startServerDetached(host: string, port: number) { - const writer = openSync(await this.fileManagerService.getLogPath(), 'a+'); + const writer = openSync(await fileManagerService.getLogPath(), 'a+'); const server = fork(join(__dirname, './../../main.js'), [], { detached: true, stdio: ['ignore', writer, writer, 'ipc'], @@ -187,7 +184,7 @@ export class CortexUsecases implements BeforeApplicationShutdown { const { apiServerHost: configApiServerHost, apiServerPort: configApiServerPort, - } = await this.fileManagerService.getConfig(); + } = await fileManagerService.getConfig(); // for backward compatibility, we didn't have the apiServerHost and apiServerPort in the config file in the past const apiServerHost = host || configApiServerHost || defaultCortexJsHost; @@ -203,7 +200,7 @@ export class CortexUsecases implements BeforeApplicationShutdown { const { apiServerHost: configApiServerHost, apiServerPort: configApiServerPort, - } = await this.fileManagerService.getConfig(); + } = await fileManagerService.getConfig(); // for backward compatibility, we didn't have the apiServerHost and apiServerPort in the config file in the past const apiServerHost = configApiServerHost || defaultCortexJsHost; @@ -215,8 +212,8 @@ export class CortexUsecases implements BeforeApplicationShutdown { } async updateApiServerConfig(host: string, port: number) { - const config = await this.fileManagerService.getConfig(); - await this.fileManagerService.writeConfigFile({ + const config = await fileManagerService.getConfig(); + await fileManagerService.writeConfigFile({ ...config, cortexCppHost: host, cortexCppPort: port, diff --git a/cortex-js/src/usecases/engines/engines.usecase.ts b/cortex-js/src/usecases/engines/engines.usecase.ts index e8f421a8d..d525f2fa4 100644 --- a/cortex-js/src/usecases/engines/engines.usecase.ts +++ b/cortex-js/src/usecases/engines/engines.usecase.ts @@ -8,7 +8,6 @@ import decompress from 'decompress'; import { exit } from 'node:process'; import { InitOptions } from '@commanders/types/init-options.interface'; import { firstValueFrom } from 'rxjs'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { rm } from 'fs/promises'; import { CORTEX_ENGINE_RELEASES_URL, @@ -23,12 +22,12 @@ import { CommonResponseDto } from '@/infrastructure/dtos/common/common-response. import { EngineStatus } from '@/domain/abstracts/engine.abstract'; import { ConfigsUsecases } from '../configs/configs.usecase'; import { defaultInstallationOptions } from '@/utils/init'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @Injectable() export class EnginesUsecases { constructor( private readonly httpService: HttpService, - private readonly fileManagerService: FileManagerService, private readonly downloadManagerService: DownloadManagerService, private readonly extensionRepository: ExtensionRepository, private readonly configsUsecases: ConfigsUsecases, @@ -86,7 +85,7 @@ export class EnginesUsecases { // Ship Llama.cpp engine by default if ( !existsSync( - join(await this.fileManagerService.getCortexCppEnginePath(), engine), + join(await fileManagerService.getCortexCppEnginePath(), engine), ) || force ) { @@ -187,7 +186,7 @@ export class EnginesUsecases { private installCudaToolkitDependency = async (cudaVersion?: string) => { const platform = process.platform === 'win32' ? 'windows' : 'linux'; - const dataFolderPath = await this.fileManagerService.getDataFolderPath(); + const dataFolderPath = await fileManagerService.getDataFolderPath(); const url = CUDA_DOWNLOAD_URL.replace( '', cudaVersion === '11' ? '11.7' : MIN_CUDA_VERSION, @@ -205,7 +204,7 @@ export class EnginesUsecases { try { await decompress( destination, - await this.fileManagerService.getCortexCppEnginePath(), + await fileManagerService.getCortexCppEnginePath(), ); } catch (e) { console.log(e); @@ -265,7 +264,7 @@ export class EnginesUsecases { ); } - const engineDir = await this.fileManagerService.getCortexCppEnginePath(); + const engineDir = await fileManagerService.getCortexCppEnginePath(); if (!existsSync(engineDir)) mkdirSync(engineDir, { recursive: true }); diff --git a/cortex-js/src/usecases/models/models.usecases.ts b/cortex-js/src/usecases/models/models.usecases.ts index 3cc4756bc..803696f4d 100644 --- a/cortex-js/src/usecases/models/models.usecases.ts +++ b/cortex-js/src/usecases/models/models.usecases.ts @@ -10,7 +10,7 @@ import { StartModelSuccessDto } from '@/infrastructure/dtos/models/start-model-s import { ExtensionRepository } from '@/domain/repositories/extension.interface'; import { EngineExtension } from '@/domain/abstracts/engine.abstract'; import { isLocalModel, normalizeModelId } from '@/utils/normalize-model-id'; -import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +import { fileManagerService, FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { AxiosError } from 'axios'; import { TelemetryUsecases } from '../telemetry/telemetry.usecases'; import { TelemetrySource } from '@/domain/telemetry/telemetry.interface'; @@ -45,7 +45,6 @@ export class ModelsUsecases { private readonly modelRepository: ModelRepository, private readonly cortexUsecases: CortexUsecases, private readonly extensionRepository: ExtensionRepository, - private readonly fileManagerService: FileManagerService, private readonly downloadManagerService: DownloadManagerService, private readonly telemetryUseCases: TelemetryUsecases, private readonly contextService: ContextService, @@ -118,7 +117,7 @@ export class ModelsUsecases { * @returns Model removal status */ async remove(id: string) { - const modelsContainerDir = await this.fileManagerService.getModelsPath(); + const modelsContainerDir = await fileManagerService.getModelsPath(); if (!existsSync(modelsContainerDir)) { return; } @@ -371,7 +370,7 @@ export class ModelsUsecases { return; } - const modelsContainerDir = await this.fileManagerService.getModelsPath(); + const modelsContainerDir = await fileManagerService.getModelsPath(); if (!existsSync(modelsContainerDir)) { mkdirSync(modelsContainerDir, { recursive: true }); @@ -425,7 +424,7 @@ export class ModelsUsecases { ) as CreateModelDto; if (model.engine === Engines.llamaCPP && model.files) { const fileUrl = join( - await this.fileManagerService.getModelsPath(), + await fileManagerService.getModelsPath(), normalizeModelId(modelId), llamaModelFile(model.files), ); @@ -434,7 +433,7 @@ export class ModelsUsecases { } else if (model.engine === Engines.llamaCPP) { model.files = [ join( - await this.fileManagerService.getModelsPath(), + await fileManagerService.getModelsPath(), normalizeModelId(modelId), basename( files.find((e) => e.rfilename.endsWith('.gguf'))?.rfilename ?? @@ -449,7 +448,7 @@ export class ModelsUsecases { if (!(await this.findOne(modelId))) await this.create(model); } else { const fileUrl = join( - await this.fileManagerService.getModelsPath(), + await fileManagerService.getModelsPath(), normalizeModelId(modelId), basename( files.find((e) => e.rfilename.endsWith('.gguf'))?.rfilename ?? diff --git a/cortex-js/src/usecases/telemetry/telemetry.module.ts b/cortex-js/src/usecases/telemetry/telemetry.module.ts index 3502d56fb..bb0db2ecb 100644 --- a/cortex-js/src/usecases/telemetry/telemetry.module.ts +++ b/cortex-js/src/usecases/telemetry/telemetry.module.ts @@ -8,8 +8,7 @@ import { FileManagerService } from '@/infrastructure/services/file-manager/file- export const telemetryProvider = { provide: 'TELEMETRY_REPOSITORY', - useFactory: (fileManagerService: FileManagerService) => - new TelemetryRepositoryImpl(fileManagerService), + useFactory: () => new TelemetryRepositoryImpl(), inject: [FileManagerService], }; From 68d15f0689a21cc0d3a4dc4fe72bd586e36fe225 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Mon, 5 Aug 2024 10:38:35 +0700 Subject: [PATCH 6/9] wip: refactor cortex client --- cortex-js/src/app.module.ts | 4 ---- .../src/infrastructure/commanders/chat.command.ts | 3 ++- .../commanders/cortex-command.commander.ts | 14 +++++++++++++- .../src/infrastructure/commanders/run.command.ts | 3 +-- .../commanders/services/chat-client.ts | 6 ++++-- .../services/file-manager/file-manager.service.ts | 13 ++++++++++++- cortex-js/src/usecases/cortex/cortex.usecases.ts | 1 - 7 files changed, 32 insertions(+), 12 deletions(-) diff --git a/cortex-js/src/app.module.ts b/cortex-js/src/app.module.ts index f2b7f5b4c..e7f8d6698 100644 --- a/cortex-js/src/app.module.ts +++ b/cortex-js/src/app.module.ts @@ -2,7 +2,6 @@ import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { MessagesModule } from './usecases/messages/messages.module'; import { ThreadsModule } from './usecases/threads/threads.module'; import { ModelsModule } from './usecases/models/models.module'; -import { DevtoolsModule } from '@nestjs/devtools-integration'; import { DatabaseModule } from './infrastructure/database/database.module'; import { ChatModule } from './usecases/chat/chat.module'; import { AssistantsModule } from './usecases/assistants/assistants.module'; @@ -32,9 +31,6 @@ import { ResourceManagerModule } from './infrastructure/services/resources-manag @Module({ imports: [ - DevtoolsModule.register({ - http: env.NODE_ENV !== 'production', - }), ConfigModule.forRoot({ isGlobal: true, envFilePath: env.NODE_ENV !== 'production' ? '.env.development' : '.env', diff --git a/cortex-js/src/infrastructure/commanders/chat.command.ts b/cortex-js/src/infrastructure/commanders/chat.command.ts index 11a122f22..b23a9b956 100644 --- a/cortex-js/src/infrastructure/commanders/chat.command.ts +++ b/cortex-js/src/infrastructure/commanders/chat.command.ts @@ -18,6 +18,7 @@ import { Cortex } from '@cortexso/cortex.js'; import { ChatClient } from './services/chat-client'; import { downloadProgress } from '@/utils/download-progress'; import { DownloadType } from '@/domain/models/download.interface'; +import { CortexClient } from './services/cortex.client'; type ChatOptions = { threadId?: string; @@ -47,13 +48,13 @@ export class ChatCommand extends BaseCommand { protected readonly contextService: ContextService, ) { super(cortexUsecases); - this.chatClient = new ChatClient(this.cortex); } async runCommand( passedParams: string[], options: ChatOptions, ): Promise { + this.chatClient = new ChatClient(this.cortex); let modelId = passedParams[0]; // First attempt to get message from input or options // Extract input from 1 to end of array diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index 67def5a06..9dde645e6 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -27,6 +27,7 @@ type ServeOptions = { logs?: boolean; dataFolder?: string; version?: boolean; + name?: string; }; @RootCommand({ @@ -60,6 +61,18 @@ export class CortexCommand extends CommandRunner { } async run(passedParams: string[], options?: ServeOptions): Promise { + if (options?.name) { + const isProfileConfigExists = fileManagerService.profileConfigExists( + options.name, + ); + if (!isProfileConfigExists) { + await fileManagerService.writeConfigFile({ + ...fileManagerService.defaultConfig(), + apiServerHost: options?.address || defaultCortexJsHost, + apiServerPort: options?.port || defaultCortexJsPort, + }); + } + } const { apiServerHost: configApiServerHost, apiServerPort: configApiServerPort, @@ -70,7 +83,6 @@ export class CortexCommand extends CommandRunner { 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; diff --git a/cortex-js/src/infrastructure/commanders/run.command.ts b/cortex-js/src/infrastructure/commanders/run.command.ts index f5a9393d1..1ae5fc9a4 100644 --- a/cortex-js/src/infrastructure/commanders/run.command.ts +++ b/cortex-js/src/infrastructure/commanders/run.command.ts @@ -38,11 +38,10 @@ export class RunCommand extends BaseCommand { private readonly inquirerService: InquirerService, ) { super(cortexUsecases); - - this.chatClient = new ChatClient(this.cortex); } async runCommand(passedParams: string[], options: RunOptions): Promise { + this.chatClient = new ChatClient(this.cortex); let modelId = passedParams[0]; const checkingSpinner = ora('Checking model...').start(); if (!modelId) { diff --git a/cortex-js/src/infrastructure/commanders/services/chat-client.ts b/cortex-js/src/infrastructure/commanders/services/chat-client.ts index f617f83bc..b22486170 100644 --- a/cortex-js/src/infrastructure/commanders/services/chat-client.ts +++ b/cortex-js/src/infrastructure/commanders/services/chat-client.ts @@ -11,8 +11,10 @@ export class ChatClient { private exitClause = 'exit()'; private userIndicator = '>> '; private exitMessage = 'Bye!'; - - constructor(readonly cortex: Cortex) {} + private cortex: Cortex; + constructor(readonly cortexInstance: Cortex) { + this.cortex = cortexInstance; + } async chat( modelId: string, 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 87bef9802..5e74b77c5 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 @@ -109,7 +109,7 @@ export class FileManagerService { } } - private defaultConfig(): Config { + public defaultConfig(): Config { // default will store at home directory const homeDir = os.homedir(); const dataFolderPath = join(homeDir, this.cortexDirectoryName); @@ -358,6 +358,17 @@ export class FileManagerService { public setConfigProfile(profile: string) { this.configProfile = profile; } + public profileConfigExists(profile: string): boolean { + const homeDir = os.homedir(); + const configPath = join(homeDir, this.configFile); + try { + const content = readFileSync(configPath, 'utf8'); + const configs = (yaml.load(content) as Record) ?? {}; + return !!configs[profile]; + } catch { + return false; + } + } } export const fileManagerService = new FileManagerService(); diff --git a/cortex-js/src/usecases/cortex/cortex.usecases.ts b/cortex-js/src/usecases/cortex/cortex.usecases.ts index 4850cac77..3e75af1aa 100644 --- a/cortex-js/src/usecases/cortex/cortex.usecases.ts +++ b/cortex-js/src/usecases/cortex/cortex.usecases.ts @@ -185,7 +185,6 @@ export class CortexUsecases implements BeforeApplicationShutdown { apiServerHost: configApiServerHost, apiServerPort: configApiServerPort, } = await fileManagerService.getConfig(); - // for backward compatibility, we didn't have the apiServerHost and apiServerPort in the config file in the past const apiServerHost = host || configApiServerHost || defaultCortexJsHost; const apiServerPort = port || configApiServerPort || defaultCortexJsPort; From c40e6128fa797f0fa4d2bea1760023a0e7998e44 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Mon, 5 Aug 2024 12:35:51 +0700 Subject: [PATCH 7/9] refactor cortex client --- cortex-js/src/command.module.ts | 2 + .../src/domain/config/config.interface.ts | 1 + .../infrastructure/commanders/base.command.ts | 7 --- .../commanders/benchmark.command.ts | 2 + .../infrastructure/commanders/chat.command.ts | 1 + .../commanders/cortex-command.commander.ts | 24 +++++--- .../commanders/embeddings.command.ts | 2 + .../commanders/engines/engines-get.command.ts | 2 + .../engines/engines-init.command.ts | 2 + .../engines/engines-list.command.ts | 2 + .../commanders/engines/engines-set.command.ts | 6 +- .../commanders/models/model-get.command.ts | 2 + .../commanders/models/model-list.command.ts | 2 + .../commanders/models/model-pull.command.ts | 2 + .../commanders/models/model-remove.command.ts | 2 + .../commanders/models/model-start.command.ts | 2 + .../commanders/models/model-stop.command.ts | 2 + .../commanders/models/model-update.command.ts | 2 + .../infrastructure/commanders/run.command.ts | 2 + .../file-manager/file-manager.service.ts | 61 +++++++++++++------ 20 files changed, 92 insertions(+), 36 deletions(-) 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/domain/config/config.interface.ts b/cortex-js/src/domain/config/config.interface.ts index b99d76600..de9a6af4e 100644 --- a/cortex-js/src/domain/config/config.interface.ts +++ b/cortex-js/src/domain/config/config.interface.ts @@ -5,4 +5,5 @@ export interface Config { // todo: will remove optional when all command request api server apiServerPort?: number; apiServerHost?: string; + isDefault: boolean; } diff --git a/cortex-js/src/infrastructure/commanders/base.command.ts b/cortex-js/src/infrastructure/commanders/base.command.ts index e0839fcb2..04840a8f0 100644 --- a/cortex-js/src/infrastructure/commanders/base.command.ts +++ b/cortex-js/src/infrastructure/commanders/base.command.ts @@ -2,12 +2,8 @@ import { CommandRunner } from 'nest-commander'; import { Injectable } from '@nestjs/common'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import ora from 'ora'; -import { CortexClient } from './services/cortex.client'; -import { fileManagerService } from '../services/file-manager/file-manager.service'; - @Injectable() export abstract class BaseCommand extends CommandRunner { - cortex: CortexClient; constructor(readonly cortexUseCases: CortexUsecases) { super(); } @@ -29,9 +25,6 @@ export abstract class BaseCommand extends CommandRunner { process.exit(1); } checkingSpinner.succeed('API server is online'); - if (!this.cortex) { - this.cortex = new CortexClient(); - } await this.runCommand(passedParam, options); } } diff --git a/cortex-js/src/infrastructure/commanders/benchmark.command.ts b/cortex-js/src/infrastructure/commanders/benchmark.command.ts index 2820ded7e..a9b012db5 100644 --- a/cortex-js/src/infrastructure/commanders/benchmark.command.ts +++ b/cortex-js/src/infrastructure/commanders/benchmark.command.ts @@ -16,6 +16,7 @@ import { defaultBenchmarkConfiguration } from '../constants/benchmark'; import { inspect } from 'util'; import { Cortex } from '@cortexso/cortex.js'; import { fileManagerService } from '../services/file-manager/file-manager.service'; +import { CortexClient } from './services/cortex.client'; @SubCommand({ name: 'benchmark', @@ -30,6 +31,7 @@ export class BenchmarkCommand extends BaseCommand { constructor( readonly cortexUsecases: CortexUsecases, 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 b23a9b956..75ff2186f 100644 --- a/cortex-js/src/infrastructure/commanders/chat.command.ts +++ b/cortex-js/src/infrastructure/commanders/chat.command.ts @@ -46,6 +46,7 @@ export class ChatCommand extends BaseCommand { private readonly telemetryUsecases: TelemetryUsecases, protected readonly cortexUsecases: CortexUsecases, protected readonly contextService: ContextService, + private readonly cortex: CortexClient, ) { super(cortexUsecases); } diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index 9dde645e6..6637847dd 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -62,16 +62,22 @@ export class CortexCommand extends CommandRunner { async run(passedParams: string[], options?: ServeOptions): Promise { if (options?.name) { - const isProfileConfigExists = fileManagerService.profileConfigExists( - options.name, - ); - if (!isProfileConfigExists) { - await fileManagerService.writeConfigFile({ + const profileConfig = fileManagerService.getProfileConfig(options.name); + await fileManagerService.writeConfigFile( + { ...fileManagerService.defaultConfig(), - apiServerHost: options?.address || defaultCortexJsHost, - apiServerPort: options?.port || defaultCortexJsPort, - }); - } + apiServerHost: + options?.address || + profileConfig?.apiServerHost || + defaultCortexJsHost, + apiServerPort: + options?.port || + profileConfig?.apiServerPort || + defaultCortexJsPort, + isDefault: true, + }, + true, + ); } const { apiServerHost: configApiServerHost, diff --git a/cortex-js/src/infrastructure/commanders/embeddings.command.ts b/cortex-js/src/infrastructure/commanders/embeddings.command.ts index bf2dc8df3..7417b52b7 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 { CortexClient } from './services/cortex.client'; interface EmbeddingCommandOptions { encoding_format?: string; @@ -24,6 +25,7 @@ export class EmbeddingCommand extends BaseCommand { constructor( private readonly inquirerService: InquirerService, readonly cortexUsecases: CortexUsecases, + readonly cortex: CortexClient, ) { super(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..827eb3270 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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: ' get', @@ -17,6 +18,7 @@ export class EnginesGetCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, + 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 c65075e5d..9e1b17508 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts @@ -9,6 +9,7 @@ import { Presets, SingleBar } from 'cli-progress'; import ora from 'ora'; import { InitEngineDto } from '@/infrastructure/dtos/engines/engines.dto'; import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: ' init', @@ -22,6 +23,7 @@ export class EnginesInitCommand extends BaseCommand { constructor( private readonly cortexUsecases: CortexUsecases, readonly contextService: ContextService, + readonly cortex: CortexClient, ) { super(cortexUsecases); } 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..196d9e747 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts @@ -4,6 +4,7 @@ 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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'list', @@ -14,6 +15,7 @@ export class EnginesListCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + 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 655c7d614..9686b3ac4 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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: ' set ', @@ -12,7 +13,10 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; }) @SetCommandContext() export class EnginesSetCommand extends BaseCommand { - constructor(readonly cortexUsecases: CortexUsecases) { + constructor( + readonly cortexUsecases: CortexUsecases, + readonly cortex: CortexClient, + ) { 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 3bc4e8fa5..0c7783793 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-get.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-get.command.ts @@ -3,6 +3,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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'get', @@ -17,6 +18,7 @@ export class ModelGetCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + 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 9e169733a..49d5fb6cf 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-list.command.ts @@ -3,6 +3,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 { CortexClient } from '../services/cortex.client'; interface ModelListOptions { format: 'table' | 'json'; @@ -13,6 +14,7 @@ export class ModelListCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + 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 179484845..1f537cf48 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts @@ -19,6 +19,7 @@ import { DownloadType } from '@/domain/models/download.interface'; import ora from 'ora'; import { isRemoteEngine } from '@/utils/normalize-model-id'; import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; +import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'pull', @@ -34,6 +35,7 @@ export class ModelPullCommand extends BaseCommand { private readonly telemetryUsecases: TelemetryUsecases, readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, + readonly cortex: CortexClient, ) { 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 52e8aefdc..78ad82878 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts @@ -3,6 +3,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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'remove', @@ -17,6 +18,7 @@ export class ModelRemoveCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + 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 f345287c6..83ed960ec 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-start.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-start.command.ts @@ -14,6 +14,7 @@ import { isRemoteEngine } from '@/utils/normalize-model-id'; import { downloadProgress } from '@/utils/download-progress'; import { DownloadType } from '@/domain/models/download.interface'; import { printLastErrorLines } from '@/utils/logs'; +import { CortexClient } from '../services/cortex.client'; type ModelStartOptions = { preset?: string; @@ -34,6 +35,7 @@ export class ModelStartCommand extends BaseCommand { private readonly inquirerService: InquirerService, readonly cortexUsecases: CortexUsecases, readonly contextService: ContextService, + 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 2eb65666a..437c76a59 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts @@ -4,6 +4,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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'stop', @@ -18,6 +19,7 @@ export class ModelStopCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + 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 a9d5f84ed..415077e08 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 { CortexClient } from '../services/cortex.client'; type UpdateOptions = { model?: string; @@ -24,6 +25,7 @@ export class ModelUpdateCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, + readonly cortex: CortexClient, ) { super(cortexUseCases); } diff --git a/cortex-js/src/infrastructure/commanders/run.command.ts b/cortex-js/src/infrastructure/commanders/run.command.ts index 1ae5fc9a4..e1d4635ec 100644 --- a/cortex-js/src/infrastructure/commanders/run.command.ts +++ b/cortex-js/src/infrastructure/commanders/run.command.ts @@ -15,6 +15,7 @@ import { isLocalFile } from '@/utils/urls'; import { parse } from 'node:path'; import { printLastErrorLines } from '@/utils/logs'; import { fileManagerService } from '../services/file-manager/file-manager.service'; +import { CortexClient } from './services/cortex.client'; type RunOptions = { threadId?: string; @@ -36,6 +37,7 @@ export class RunCommand extends BaseCommand { constructor( protected readonly cortexUsecases: CortexUsecases, private readonly inquirerService: InquirerService, + private readonly cortex: CortexClient, ) { super(cortexUsecases); } 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 5e74b77c5..a5eeea646 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 @@ -45,41 +45,59 @@ export class FileManagerService { async getConfig(dataFolderPath?: string): Promise { const homeDir = os.homedir(); const configPath = join(homeDir, this.configFile); - const config = this.defaultConfig(); - const dataFolderPathUsed = dataFolderPath || config.dataFolderPath; + const defaultConfig = this.defaultConfig(); + const dataFolderPathUsed = dataFolderPath || defaultConfig.dataFolderPath; if (!existsSync(configPath) || !existsSync(dataFolderPathUsed)) { await this.createFolderIfNotExist(dataFolderPathUsed); - await this.writeConfigFile(config); - return config; + await this.writeConfigFile(defaultConfig); + return defaultConfig; } try { const content = await promises.readFile(configPath, 'utf8'); - const configs = yaml.load(content) as Record; - const config = configs?.[this.configProfile] ?? {}; + const configs = (yaml.load(content) as Record) ?? { + [this.configProfile]: defaultConfig, + }; + const configKey = Object.keys(configs).find( + (key) => configs[key]?.isDefault, + ); return { - ...this.defaultConfig(), - ...config, + ...defaultConfig, + ...(configs[configKey || this.configProfile] ?? {}), }; } catch (error) { console.warn('Error reading config file. Using default config.'); console.warn(error); await this.createFolderIfNotExist(dataFolderPathUsed); - await this.writeConfigFile(config); - return config; + await this.writeConfigFile(defaultConfig); + return defaultConfig; } } - async writeConfigFile(config: Config & object): Promise { + async writeConfigFile( + config: Config & object, + setDefault: boolean = false, + ): Promise { const homeDir = os.homedir(); const configPath = join(homeDir, this.configFile); // write config to file as yaml + if (!existsSync(configPath)) { + await promises.writeFile(configPath, '', 'utf8'); + } const content = await promises.readFile(configPath, 'utf8'); const currentConfig = yaml.load(content) as Record; + if (setDefault) { + for (const key in currentConfig) { + currentConfig[key].isDefault = false; + } + } const configString = yaml.dump({ ...currentConfig, - [this.configProfile]: config, + [this.configProfile]: { + ...config, + isDefault: setDefault, + }, }); await promises.writeFile(configPath, configString, 'utf8'); } @@ -113,13 +131,13 @@ export class FileManagerService { // default will store at home directory const homeDir = os.homedir(); const dataFolderPath = join(homeDir, this.cortexDirectoryName); - return { dataFolderPath, cortexCppHost: defaultCortexCppHost, cortexCppPort: defaultCortexCppPort, apiServerHost: defaultCortexJsHost, apiServerPort: defaultCortexJsPort, + isDefault: true, }; } @@ -347,26 +365,31 @@ export class FileManagerService { try { const content = readFileSync(configPath, 'utf8'); const configs = (yaml.load(content) as Record) ?? {}; - config = configs?.[this.configProfile] ?? config; + const defaultConfig = Object.values(configs).find( + (currentConfig) => currentConfig.isDefault, + ); + if (defaultConfig) { + config = defaultConfig; + } } catch {} return { - host: config.apiServerHost ?? 'localhost', - port: config.apiServerPort ?? 1337, + host: config.apiServerHost ?? defaultCortexJsHost, + port: config.apiServerPort ?? defaultCortexJsPort, }; } public setConfigProfile(profile: string) { this.configProfile = profile; } - public profileConfigExists(profile: string): boolean { + public getProfileConfig(profile: string): (Config & object) | null { const homeDir = os.homedir(); const configPath = join(homeDir, this.configFile); try { const content = readFileSync(configPath, 'utf8'); const configs = (yaml.load(content) as Record) ?? {}; - return !!configs[profile]; + return configs[profile]; } catch { - return false; + return null; } } } From 02fa9dc77c6003827dfee363a9aa6adfc59034ad Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Mon, 5 Aug 2024 13:31:38 +0700 Subject: [PATCH 8/9] Revert "refactor cortex client" This reverts commit c40e6128fa797f0fa4d2bea1760023a0e7998e44. --- cortex-js/src/command.module.ts | 2 - .../src/domain/config/config.interface.ts | 1 - .../infrastructure/commanders/base.command.ts | 7 +++ .../commanders/benchmark.command.ts | 2 - .../infrastructure/commanders/chat.command.ts | 1 - .../commanders/cortex-command.commander.ts | 24 +++----- .../commanders/embeddings.command.ts | 2 - .../commanders/engines/engines-get.command.ts | 2 - .../engines/engines-init.command.ts | 2 - .../engines/engines-list.command.ts | 2 - .../commanders/engines/engines-set.command.ts | 6 +- .../commanders/models/model-get.command.ts | 2 - .../commanders/models/model-list.command.ts | 2 - .../commanders/models/model-pull.command.ts | 2 - .../commanders/models/model-remove.command.ts | 2 - .../commanders/models/model-start.command.ts | 2 - .../commanders/models/model-stop.command.ts | 2 - .../commanders/models/model-update.command.ts | 2 - .../infrastructure/commanders/run.command.ts | 2 - .../file-manager/file-manager.service.ts | 61 ++++++------------- 20 files changed, 36 insertions(+), 92 deletions(-) diff --git a/cortex-js/src/command.module.ts b/cortex-js/src/command.module.ts index fb35a5be9..242136d07 100644 --- a/cortex-js/src/command.module.ts +++ b/cortex-js/src/command.module.ts @@ -28,7 +28,6 @@ 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: [ @@ -42,7 +41,6 @@ import { CortexClientModule } from './infrastructure/commanders/services/cortex. FileManagerModule, TelemetryModule, ContextModule, - CortexClientModule, ], providers: [ CortexCommand, diff --git a/cortex-js/src/domain/config/config.interface.ts b/cortex-js/src/domain/config/config.interface.ts index de9a6af4e..b99d76600 100644 --- a/cortex-js/src/domain/config/config.interface.ts +++ b/cortex-js/src/domain/config/config.interface.ts @@ -5,5 +5,4 @@ export interface Config { // todo: will remove optional when all command request api server apiServerPort?: number; apiServerHost?: string; - isDefault: boolean; } diff --git a/cortex-js/src/infrastructure/commanders/base.command.ts b/cortex-js/src/infrastructure/commanders/base.command.ts index 04840a8f0..e0839fcb2 100644 --- a/cortex-js/src/infrastructure/commanders/base.command.ts +++ b/cortex-js/src/infrastructure/commanders/base.command.ts @@ -2,8 +2,12 @@ import { CommandRunner } from 'nest-commander'; import { Injectable } from '@nestjs/common'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import ora from 'ora'; +import { CortexClient } from './services/cortex.client'; +import { fileManagerService } from '../services/file-manager/file-manager.service'; + @Injectable() export abstract class BaseCommand extends CommandRunner { + cortex: CortexClient; constructor(readonly cortexUseCases: CortexUsecases) { super(); } @@ -25,6 +29,9 @@ export abstract class BaseCommand extends CommandRunner { process.exit(1); } checkingSpinner.succeed('API server is online'); + if (!this.cortex) { + this.cortex = new CortexClient(); + } await this.runCommand(passedParam, options); } } diff --git a/cortex-js/src/infrastructure/commanders/benchmark.command.ts b/cortex-js/src/infrastructure/commanders/benchmark.command.ts index a9b012db5..2820ded7e 100644 --- a/cortex-js/src/infrastructure/commanders/benchmark.command.ts +++ b/cortex-js/src/infrastructure/commanders/benchmark.command.ts @@ -16,7 +16,6 @@ import { defaultBenchmarkConfiguration } from '../constants/benchmark'; import { inspect } from 'util'; import { Cortex } from '@cortexso/cortex.js'; import { fileManagerService } from '../services/file-manager/file-manager.service'; -import { CortexClient } from './services/cortex.client'; @SubCommand({ name: 'benchmark', @@ -31,7 +30,6 @@ export class BenchmarkCommand extends BaseCommand { constructor( readonly cortexUsecases: CortexUsecases, 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 75ff2186f..b23a9b956 100644 --- a/cortex-js/src/infrastructure/commanders/chat.command.ts +++ b/cortex-js/src/infrastructure/commanders/chat.command.ts @@ -46,7 +46,6 @@ export class ChatCommand extends BaseCommand { private readonly telemetryUsecases: TelemetryUsecases, protected readonly cortexUsecases: CortexUsecases, protected readonly contextService: ContextService, - private readonly cortex: CortexClient, ) { super(cortexUsecases); } diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index 6637847dd..9dde645e6 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -62,22 +62,16 @@ export class CortexCommand extends CommandRunner { async run(passedParams: string[], options?: ServeOptions): Promise { if (options?.name) { - const profileConfig = fileManagerService.getProfileConfig(options.name); - await fileManagerService.writeConfigFile( - { - ...fileManagerService.defaultConfig(), - apiServerHost: - options?.address || - profileConfig?.apiServerHost || - defaultCortexJsHost, - apiServerPort: - options?.port || - profileConfig?.apiServerPort || - defaultCortexJsPort, - isDefault: true, - }, - true, + const isProfileConfigExists = fileManagerService.profileConfigExists( + options.name, ); + if (!isProfileConfigExists) { + await fileManagerService.writeConfigFile({ + ...fileManagerService.defaultConfig(), + apiServerHost: options?.address || defaultCortexJsHost, + apiServerPort: options?.port || defaultCortexJsPort, + }); + } } const { apiServerHost: configApiServerHost, diff --git a/cortex-js/src/infrastructure/commanders/embeddings.command.ts b/cortex-js/src/infrastructure/commanders/embeddings.command.ts index 7417b52b7..bf2dc8df3 100644 --- a/cortex-js/src/infrastructure/commanders/embeddings.command.ts +++ b/cortex-js/src/infrastructure/commanders/embeddings.command.ts @@ -4,7 +4,6 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { BaseCommand } from './base.command'; import { Cortex } from '@cortexso/cortex.js'; import ora from 'ora'; -import { CortexClient } from './services/cortex.client'; interface EmbeddingCommandOptions { encoding_format?: string; @@ -25,7 +24,6 @@ export class EmbeddingCommand extends BaseCommand { constructor( private readonly inquirerService: InquirerService, readonly cortexUsecases: CortexUsecases, - readonly cortex: CortexClient, ) { super(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 827eb3270..912087a6d 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,6 @@ 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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: ' get', @@ -18,7 +17,6 @@ export class EnginesGetCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, - 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 9e1b17508..c65075e5d 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts @@ -9,7 +9,6 @@ import { Presets, SingleBar } from 'cli-progress'; import ora from 'ora'; import { InitEngineDto } from '@/infrastructure/dtos/engines/engines.dto'; import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; -import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: ' init', @@ -23,7 +22,6 @@ export class EnginesInitCommand extends BaseCommand { constructor( private readonly cortexUsecases: CortexUsecases, readonly contextService: ContextService, - readonly cortex: CortexClient, ) { super(cortexUsecases); } 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 196d9e747..25f85949f 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts @@ -4,7 +4,6 @@ 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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'list', @@ -15,7 +14,6 @@ export class EnginesListCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - 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 9686b3ac4..655c7d614 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,6 @@ import { SubCommand } from 'nest-commander'; import { SetCommandContext } from '../decorators/CommandContext'; import { BaseCommand } from '../base.command'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; -import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: ' set ', @@ -13,10 +12,7 @@ import { CortexClient } from '../services/cortex.client'; }) @SetCommandContext() export class EnginesSetCommand extends BaseCommand { - constructor( - readonly cortexUsecases: CortexUsecases, - readonly cortex: CortexClient, - ) { + 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 0c7783793..3bc4e8fa5 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-get.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-get.command.ts @@ -3,7 +3,6 @@ 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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'get', @@ -18,7 +17,6 @@ export class ModelGetCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - 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 49d5fb6cf..9e169733a 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-list.command.ts @@ -3,7 +3,6 @@ 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 { CortexClient } from '../services/cortex.client'; interface ModelListOptions { format: 'table' | 'json'; @@ -14,7 +13,6 @@ export class ModelListCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - 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 1f537cf48..179484845 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-pull.command.ts @@ -19,7 +19,6 @@ import { DownloadType } from '@/domain/models/download.interface'; import ora from 'ora'; import { isRemoteEngine } from '@/utils/normalize-model-id'; import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; -import { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'pull', @@ -35,7 +34,6 @@ export class ModelPullCommand extends BaseCommand { private readonly telemetryUsecases: TelemetryUsecases, readonly contextService: ContextService, readonly cortexUsecases: CortexUsecases, - readonly cortex: CortexClient, ) { 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 78ad82878..52e8aefdc 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-remove.command.ts @@ -3,7 +3,6 @@ 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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'remove', @@ -18,7 +17,6 @@ export class ModelRemoveCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - 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 83ed960ec..f345287c6 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-start.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-start.command.ts @@ -14,7 +14,6 @@ import { isRemoteEngine } from '@/utils/normalize-model-id'; import { downloadProgress } from '@/utils/download-progress'; import { DownloadType } from '@/domain/models/download.interface'; import { printLastErrorLines } from '@/utils/logs'; -import { CortexClient } from '../services/cortex.client'; type ModelStartOptions = { preset?: string; @@ -35,7 +34,6 @@ export class ModelStartCommand extends BaseCommand { private readonly inquirerService: InquirerService, readonly cortexUsecases: CortexUsecases, readonly contextService: ContextService, - 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 437c76a59..2eb65666a 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-stop.command.ts @@ -4,7 +4,6 @@ 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 { CortexClient } from '../services/cortex.client'; @SubCommand({ name: 'stop', @@ -19,7 +18,6 @@ export class ModelStopCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - 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 415077e08..a9d5f84ed 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,6 @@ 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 { CortexClient } from '../services/cortex.client'; type UpdateOptions = { model?: string; @@ -25,7 +24,6 @@ export class ModelUpdateCommand extends BaseCommand { constructor( readonly contextService: ContextService, readonly cortexUseCases: CortexUsecases, - readonly cortex: CortexClient, ) { super(cortexUseCases); } diff --git a/cortex-js/src/infrastructure/commanders/run.command.ts b/cortex-js/src/infrastructure/commanders/run.command.ts index e1d4635ec..1ae5fc9a4 100644 --- a/cortex-js/src/infrastructure/commanders/run.command.ts +++ b/cortex-js/src/infrastructure/commanders/run.command.ts @@ -15,7 +15,6 @@ import { isLocalFile } from '@/utils/urls'; import { parse } from 'node:path'; import { printLastErrorLines } from '@/utils/logs'; import { fileManagerService } from '../services/file-manager/file-manager.service'; -import { CortexClient } from './services/cortex.client'; type RunOptions = { threadId?: string; @@ -37,7 +36,6 @@ export class RunCommand extends BaseCommand { constructor( protected readonly cortexUsecases: CortexUsecases, private readonly inquirerService: InquirerService, - private readonly cortex: CortexClient, ) { super(cortexUsecases); } 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 a5eeea646..5e74b77c5 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 @@ -45,59 +45,41 @@ export class FileManagerService { async getConfig(dataFolderPath?: string): Promise { const homeDir = os.homedir(); const configPath = join(homeDir, this.configFile); - const defaultConfig = this.defaultConfig(); - const dataFolderPathUsed = dataFolderPath || defaultConfig.dataFolderPath; + const config = this.defaultConfig(); + const dataFolderPathUsed = dataFolderPath || config.dataFolderPath; if (!existsSync(configPath) || !existsSync(dataFolderPathUsed)) { await this.createFolderIfNotExist(dataFolderPathUsed); - await this.writeConfigFile(defaultConfig); - return defaultConfig; + await this.writeConfigFile(config); + return config; } try { const content = await promises.readFile(configPath, 'utf8'); - const configs = (yaml.load(content) as Record) ?? { - [this.configProfile]: defaultConfig, - }; - const configKey = Object.keys(configs).find( - (key) => configs[key]?.isDefault, - ); + const configs = yaml.load(content) as Record; + const config = configs?.[this.configProfile] ?? {}; return { - ...defaultConfig, - ...(configs[configKey || this.configProfile] ?? {}), + ...this.defaultConfig(), + ...config, }; } catch (error) { console.warn('Error reading config file. Using default config.'); console.warn(error); await this.createFolderIfNotExist(dataFolderPathUsed); - await this.writeConfigFile(defaultConfig); - return defaultConfig; + await this.writeConfigFile(config); + return config; } } - async writeConfigFile( - config: Config & object, - setDefault: boolean = false, - ): Promise { + async writeConfigFile(config: Config & object): Promise { const homeDir = os.homedir(); const configPath = join(homeDir, this.configFile); // write config to file as yaml - if (!existsSync(configPath)) { - await promises.writeFile(configPath, '', 'utf8'); - } const content = await promises.readFile(configPath, 'utf8'); const currentConfig = yaml.load(content) as Record; - if (setDefault) { - for (const key in currentConfig) { - currentConfig[key].isDefault = false; - } - } const configString = yaml.dump({ ...currentConfig, - [this.configProfile]: { - ...config, - isDefault: setDefault, - }, + [this.configProfile]: config, }); await promises.writeFile(configPath, configString, 'utf8'); } @@ -131,13 +113,13 @@ export class FileManagerService { // default will store at home directory const homeDir = os.homedir(); const dataFolderPath = join(homeDir, this.cortexDirectoryName); + return { dataFolderPath, cortexCppHost: defaultCortexCppHost, cortexCppPort: defaultCortexCppPort, apiServerHost: defaultCortexJsHost, apiServerPort: defaultCortexJsPort, - isDefault: true, }; } @@ -365,31 +347,26 @@ export class FileManagerService { try { const content = readFileSync(configPath, 'utf8'); const configs = (yaml.load(content) as Record) ?? {}; - const defaultConfig = Object.values(configs).find( - (currentConfig) => currentConfig.isDefault, - ); - if (defaultConfig) { - config = defaultConfig; - } + config = configs?.[this.configProfile] ?? config; } catch {} return { - host: config.apiServerHost ?? defaultCortexJsHost, - port: config.apiServerPort ?? defaultCortexJsPort, + host: config.apiServerHost ?? 'localhost', + port: config.apiServerPort ?? 1337, }; } public setConfigProfile(profile: string) { this.configProfile = profile; } - public getProfileConfig(profile: string): (Config & object) | null { + public profileConfigExists(profile: string): boolean { const homeDir = os.homedir(); const configPath = join(homeDir, this.configFile); try { const content = readFileSync(configPath, 'utf8'); const configs = (yaml.load(content) as Record) ?? {}; - return configs[profile]; + return !!configs[profile]; } catch { - return null; + return false; } } } From 0c0d80533af19cc4d55940bc2f87db431c31077f Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Mon, 5 Aug 2024 13:52:48 +0700 Subject: [PATCH 9/9] fix failed test --- .../services/file-manager/file-manager.service.ts | 3 +++ 1 file changed, 3 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 5e74b77c5..5975d3b2a 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 @@ -75,6 +75,9 @@ export class FileManagerService { const configPath = join(homeDir, this.configFile); // write config to file as yaml + if (!existsSync(configPath)) { + await promises.writeFile(configPath, '', 'utf8'); + } const content = await promises.readFile(configPath, 'utf8'); const currentConfig = yaml.load(content) as Record; const configString = yaml.dump({