Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cortex-js/src/app.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions cortex-js/src/command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -42,7 +41,6 @@ import { CortexClientModule } from './infrastructure/commanders/services/cortex.
FileManagerModule,
TelemetryModule,
ContextModule,
CortexClientModule,
],
providers: [
CortexCommand,
Expand Down
6 changes: 6 additions & 0 deletions cortex-js/src/infrastructure/commanders/base.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +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();
}
Expand All @@ -26,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);
}
}
11 changes: 4 additions & 7 deletions cortex-js/src/infrastructure/commanders/benchmark.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -16,7 +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 { CortexClient } from './services/cortex.client';
import { fileManagerService } from '../services/file-manager/file-manager.service';

@SubCommand({
name: 'benchmark',
Expand All @@ -29,10 +28,8 @@ import { CortexClient } from './services/cortex.client';
})
export class BenchmarkCommand extends BaseCommand {
constructor(
private readonly cortexUsecases: CortexUsecases,
private readonly fileService: FileManagerService,
readonly cortexUsecases: CortexUsecases,
private readonly telemetryUsecases: TelemetryUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}
Expand Down Expand Up @@ -108,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(
Expand Down Expand Up @@ -301,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({
Expand Down
14 changes: 7 additions & 7 deletions cortex-js/src/infrastructure/commanders/chat.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ 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';
import { downloadProgress } from '@/utils/download-progress';
import { CortexClient } from './services/cortex.client';
import { DownloadType } from '@/domain/models/download.interface';
import { CortexClient } from './services/cortex.client';

type ChatOptions = {
threadId?: string;
Expand All @@ -44,19 +44,17 @@ 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,
protected readonly cortex: CortexClient,
) {
super(cortexUsecases);
this.chatClient = new ChatClient(this.cortex);
}

async runCommand(
passedParams: string[],
options: ChatOptions,
): Promise<void> {
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
Expand Down Expand Up @@ -89,7 +87,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);
Expand All @@ -107,7 +107,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -27,6 +27,7 @@ type ServeOptions = {
logs?: boolean;
dataFolder?: string;
version?: boolean;
name?: string;
};

@RootCommand({
Expand Down Expand Up @@ -54,24 +55,34 @@ export class CortexCommand extends CommandRunner {
configPort: number;
constructor(
readonly contextService: ContextService,
readonly fileManagerService: FileManagerService,
readonly cortexUseCases: CortexUsecases,
) {
super();
}

async run(passedParams: string[], options?: ServeOptions): Promise<void> {
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,
} = await this.fileManagerService.getConfig();
} = await fileManagerService.getConfig();

this.configHost = configApiServerHost || defaultCortexJsHost;
this.configPort = configApiServerPort || defaultCortexJsPort;

this.host = options?.address || configApiServerHost || defaultCortexJsHost;
this.port = options?.port || configApiServerPort || defaultCortexJsPort;

const showLogs = options?.logs || false;
const showVersion = options?.version || false;
const dataFolderPath = options?.dataFolder;
Expand All @@ -85,7 +96,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) => {
Expand All @@ -103,12 +114,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();
Expand All @@ -124,7 +135,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,
Expand All @@ -134,7 +145,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?`);
Expand Down Expand Up @@ -181,4 +192,12 @@ export class CortexCommand extends CommandRunner {
parseVersion() {
return true;
}
@Option({
flags: '-n, --name <name>',
description: 'Name of the process',
})
parseName(value: string) {
fileManagerService.setConfigProfile(value);
return value;
}
}
2 changes: 0 additions & 2 deletions cortex-js/src/infrastructure/commanders/embeddings.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +23,6 @@ interface EmbeddingCommandOptions {
export class EmbeddingCommand extends BaseCommand {
constructor(
private readonly inquirerService: InquirerService,
private readonly cortex: CortexClient,
readonly cortexUsecases: CortexUsecases,
) {
super(cortexUsecases);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<name> get',
Expand All @@ -18,7 +17,6 @@ export class EnginesGetCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUsecases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +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 { CortexClient } from '../services/cortex.client';
import ora from 'ora';
import { InitEngineDto } from '@/infrastructure/dtos/engines/engines.dto';
import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';

@SubCommand({
name: '<name> init',
Expand All @@ -22,9 +21,7 @@ import { InitEngineDto } from '@/infrastructure/dtos/engines/engines.dto';
export class EnginesInitCommand extends BaseCommand {
constructor(
private readonly cortexUsecases: CortexUsecases,
private readonly fileManagerService: FileManagerService,
readonly contextService: ContextService,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}
Expand All @@ -41,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -15,7 +14,6 @@ export class EnginesListCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<name> set <config> <value>',
Expand All @@ -13,10 +12,7 @@ import { CortexClient } from '../services/cortex.client';
})
@SetCommandContext()
export class EnginesSetCommand extends BaseCommand {
constructor(
readonly cortexUsecases: CortexUsecases,
private readonly cortex: CortexClient,
) {
constructor(readonly cortexUsecases: CortexUsecases) {
super(cortexUsecases);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -18,7 +17,6 @@ export class ModelGetCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
Expand Down
Loading