Skip to content

Commit

Permalink
chore: enhance error output (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Jul 26, 2024
1 parent 75c3f64 commit 5dc16c6
Show file tree
Hide file tree
Showing 28 changed files with 114 additions and 160 deletions.
2 changes: 2 additions & 0 deletions cortex-js/src/command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -41,6 +42,7 @@ import { EnginesSetCommand } from './infrastructure/commanders/engines/engines-s
FileManagerModule,
TelemetryModule,
ContextModule,
CortexClientModule,
],
providers: [
CortexCommand,
Expand Down
2 changes: 1 addition & 1 deletion cortex-js/src/extensions/extensions.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import OpenAIEngineExtension from './openai.engine';
import { HttpModule, HttpService } from '@nestjs/axios';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
import { ConfigsModule } from '@/usecases/configs/configs.module';
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';
import { EventEmitter2 } from '@nestjs/event-emitter';
import AnthropicEngineExtension from './anthropic.engine';

const provider = {
Expand Down
5 changes: 3 additions & 2 deletions cortex-js/src/infrastructure/commanders/base.command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CommandRunner } from 'nest-commander';
import { Injectable } from '@nestjs/common';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import Cortex from '@cortexso/cortex.js';
import ora from 'ora';

@Injectable()
Expand All @@ -21,7 +20,9 @@ export abstract class BaseCommand extends CommandRunner {
const checkingSpinner = ora('Checking API server online...').start();
const result = await this.cortexUseCases.isAPIServerOnline();
if (!result) {
checkingSpinner.fail('API server is offline');
checkingSpinner.fail(
'API server is offline. Please run "cortex" before running this command',
);
process.exit(1);
}
checkingSpinner.succeed('API server is online');
Expand Down
30 changes: 0 additions & 30 deletions cortex-js/src/infrastructure/commanders/base.subcommand.ts

This file was deleted.

5 changes: 3 additions & 2 deletions cortex-js/src/infrastructure/commanders/benchmark.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { BenchmarkHardware } from '@/domain/telemetry/telemetry.interface';
import { defaultBenchmarkConfiguration } from '../constants/benchmark';
import { inspect } from 'util';
import { Cortex } from '@cortexso/cortex.js';
import { BaseSubCommand } from './base.subcommand';
import { CortexClient } from './services/cortex.client';

@SubCommand({
name: 'benchmark',
Expand All @@ -27,11 +27,12 @@ import { BaseSubCommand } from './base.subcommand';
description:
'Benchmark and analyze the performance of a specific AI model using a variety of system resources',
})
export class BenchmarkCommand extends BaseSubCommand {
export class BenchmarkCommand extends BaseCommand {
constructor(
private readonly cortexUsecases: CortexUsecases,
private readonly fileService: FileManagerService,
private readonly telemetryUsecases: TelemetryUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}
Expand Down
7 changes: 4 additions & 3 deletions cortex-js/src/infrastructure/commanders/chat.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { isRemoteEngine } from '@/utils/normalize-model-id';
import { Cortex } from '@cortexso/cortex.js';
import { ChatClient } from './services/chat-client';
import { downloadModelProgress } from '@/utils/pull-model';
import { BaseSubCommand } from './base.subcommand';
import { CortexClient } from './services/cortex.client';

type ChatOptions = {
threadId?: string;
Expand All @@ -37,7 +37,7 @@ type ChatOptions = {
},
})
@SetCommandContext()
export class ChatCommand extends BaseSubCommand {
export class ChatCommand extends BaseCommand {
chatClient: ChatClient;

constructor(
Expand All @@ -46,6 +46,7 @@ export class ChatCommand extends BaseSubCommand {
private readonly fileService: FileManagerService,
protected readonly cortexUsecases: CortexUsecases,
protected readonly contextService: ContextService,
protected readonly cortex: CortexClient,
) {
super(cortexUsecases);
this.chatClient = new ChatClient(this.cortex);
Expand All @@ -66,7 +67,7 @@ export class ChatCommand extends BaseSubCommand {
// first input might be message input
message = passedParams.length
? passedParams.join(' ')
: options.message ?? '';
: (options.message ?? '');
// If model ID is not provided, prompt user to select from running models
const { data: models } = await this.cortex.models.list();
if (models.length === 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { FileManagerService } from '../services/file-manager/file-manager.servic
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { ServeStopCommand } from './serve-stop.command';
import ora from 'ora';
import { printSlogan } from '@/utils/logo';
import { EnginesSetCommand } from './engines/engines-set.command';

type ServeOptions = {
Expand Down Expand Up @@ -77,7 +76,6 @@ export class CortexCommand extends CommandRunner {
const showVersion = options?.version || false;
const dataFolderPath = options?.dataFolder;
if (showVersion) {
printSlogan();
console.log('\n');
console.log(`Cortex CLI - v${pkg.version}`);
console.log(chalk.blue(`Github: ${pkg.homepage}`));
Expand Down
5 changes: 3 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,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from './base.command';
import { Cortex } from '@cortexso/cortex.js';
import ora from 'ora';
import { BaseSubCommand } from './base.subcommand';
import { CortexClient } from './services/cortex.client';

interface EmbeddingCommandOptions {
encoding_format?: string;
Expand All @@ -21,9 +21,10 @@ interface EmbeddingCommandOptions {
'Model to use for embedding. If not provided, it will prompt to select from running models.',
},
})
export class EmbeddingCommand extends BaseSubCommand {
export class EmbeddingCommand extends BaseCommand {
constructor(
private readonly inquirerService: InquirerService,
private readonly cortex: CortexClient,
readonly cortexUsecases: CortexUsecases,
) {
super(cortexUsecases);
Expand Down
2 changes: 1 addition & 1 deletion cortex-js/src/infrastructure/commanders/engines.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class EnginesCommand extends BaseCommand {
) {
const commandInstance = this.moduleRef.get(commandClass, { strict: false });
if (commandInstance) {
await commandInstance.run(params, options);
await commandInstance.runCommand(params, options);
} else {
console.error('Command not found.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ContextService } from '@/infrastructure/services/context/context.servic
import { EngineNamesMap, Engines } from '../types/engine.interface';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: '<name> get',
Expand All @@ -14,10 +14,11 @@ import { BaseSubCommand } from '../base.subcommand';
},
})
@SetCommandContext()
export class EnginesGetCommand extends BaseSubCommand {
export class EnginesGetCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUsecases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FileManagerService } from '@/infrastructure/services/file-manager/file-
import { BaseCommand } from '../base.command';
import { defaultInstallationOptions } from '@/utils/init';
import { Presets, SingleBar } from 'cli-progress';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: '<name> init',
Expand All @@ -18,11 +18,12 @@ import { BaseSubCommand } from '../base.subcommand';
},
})
@SetCommandContext()
export class EnginesInitCommand extends BaseSubCommand {
export class EnginesInitCommand extends BaseCommand {
constructor(
private readonly cortexUsecases: CortexUsecases,
private readonly fileManagerService: FileManagerService,
readonly contextService: ContextService,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}
Expand All @@ -47,29 +48,28 @@ export class EnginesInitCommand extends BaseSubCommand {
await this.cortexUsecases.stopCortex();
}
console.log(`Installing engine ${engine}...`);
await this.cortex.engines
.init(engine, params)
const response = await this.cortex.events.downloadEvent()

const progressBar = new SingleBar({}, Presets.shades_classic);
progressBar.start(100, 0);

for await (const stream of response) {
if (stream.length) {
const data = stream[0] as any;
if (data.status === 'downloaded') break;
let totalBytes = 0;
let totalTransferred = 0;
data.children.forEach((child: any) => {
totalBytes += child.size.total;
totalTransferred += child.size.transferred;
});
progressBar.update(Math.floor((totalTransferred / totalBytes) * 100));
}
await this.cortex.engines.init(engine, params);
const response = await this.cortex.events.downloadEvent();

const progressBar = new SingleBar({}, Presets.shades_classic);
progressBar.start(100, 0);

for await (const stream of response) {
if (stream.length) {
const data = stream[0] as any;
if (data.status === 'downloaded') break;
let totalBytes = 0;
let totalTransferred = 0;
data.children.forEach((child: any) => {
totalBytes += child.size.total;
totalTransferred += child.size.transferred;
});
progressBar.update(Math.floor((totalTransferred / totalBytes) * 100));
}
progressBar.stop();
console.log('Engine installed successfully');
process.exit(0);
}
progressBar.stop();
console.log('Engine installed successfully');
process.exit(0);
}

@Option({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import { ContextService } from '@/infrastructure/services/context/context.servic
import { EngineNamesMap } from '../types/engine.interface';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from '../base.command';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: 'list',
description: 'Get all cortex engines',
})
@SetCommandContext()
export class EnginesListCommand extends BaseSubCommand {
export class EnginesListCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SubCommand } from 'nest-commander';
import { SetCommandContext } from '../decorators/CommandContext';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: '<name> set <config> <value>',
Expand All @@ -12,8 +12,11 @@ import { BaseSubCommand } from '../base.subcommand';
},
})
@SetCommandContext()
export class EnginesSetCommand extends BaseSubCommand {
constructor(readonly cortexUsecases: CortexUsecases) {
export class EnginesSetCommand extends BaseCommand {
constructor(
readonly cortexUsecases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}

Expand Down
2 changes: 1 addition & 1 deletion cortex-js/src/infrastructure/commanders/models.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class ModelsCommand extends BaseCommand {
) {
const commandInstance = this.moduleRef.get(commandClass, { strict: false });
if (commandInstance) {
await commandInstance.run(params, options);
await commandInstance.runCommand(params, options);
} else {
console.error('Command not found.');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { SubCommand } from 'nest-commander';
import { exit } from 'node:process';
import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: 'get',
Expand All @@ -15,10 +14,11 @@ import { BaseSubCommand } from '../base.subcommand';
},
})
@SetCommandContext()
export class ModelGetCommand extends BaseSubCommand {
export class ModelGetCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

interface ModelListOptions {
format: 'table' | 'json';
}
@SubCommand({ name: 'list', description: 'List all models locally.' })
@SetCommandContext()
export class ModelListCommand extends BaseSubCommand {
export class ModelListCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Engines } from '../types/engine.interface';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from '../base.command';
import { downloadModelProgress } from '@/utils/pull-model';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: 'pull',
Expand All @@ -27,11 +27,12 @@ import { BaseSubCommand } from '../base.subcommand';
'Download a model from a registry. Working with HuggingFace repositories. For available models, please visit https://huggingface.co/cortexso',
})
@SetCommandContext()
export class ModelPullCommand extends BaseSubCommand {
export class ModelPullCommand extends BaseCommand {
constructor(
private readonly fileService: FileManagerService,
readonly contextService: ContextService,
private readonly telemetryUsecases: TelemetryUsecases,
private readonly cortex: CortexClient,
readonly contextService: ContextService,
readonly cortexUsecases: CortexUsecases,
) {
super(cortexUsecases);
Expand Down
Loading

0 comments on commit 5dc16c6

Please sign in to comment.