Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 9857448

Browse files
Remove serve command (#896)
1 parent dc57195 commit 9857448

38 files changed

+558
-273
lines changed

cortex-js/src/command.module.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { ModelsModule } from './usecases/models/models.module';
33
import { DatabaseModule } from './infrastructure/database/database.module';
44
import { ConfigModule } from '@nestjs/config';
55
import { CortexModule } from './usecases/cortex/cortex.module';
6-
import { ServeCommand } from './infrastructure/commanders/serve.command';
76
import { ModelsCommand } from './infrastructure/commanders/models.command';
87
import { ExtensionModule } from './infrastructure/repositories/extensions/extension.module';
98
import { HttpModule } from '@nestjs/axios';
@@ -45,7 +44,6 @@ import { EnginesListCommand } from './infrastructure/commanders/engines/engines-
4544
import { EnginesGetCommand } from './infrastructure/commanders/engines/engines-get.command';
4645
import { EnginesInitCommand } from './infrastructure/commanders/engines/engines-init.command';
4746

48-
4947
@Module({
5048
imports: [
5149
ConfigModule.forRoot({
@@ -73,7 +71,6 @@ import { EnginesInitCommand } from './infrastructure/commanders/engines/engines-
7371
providers: [
7472
CortexCommand,
7573
ModelsCommand,
76-
ServeCommand,
7774
ChatCommand,
7875
PSCommand,
7976
KillCommand,

cortex-js/src/domain/abstracts/oai.abstract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export abstract class OAIEngineExtension extends EngineExtension {
2525
const additionalHeaders = _.omit(headers, [
2626
'content-type',
2727
'authorization',
28-
'content-length'
28+
'content-length',
2929
]);
3030
const response = await firstValueFrom(
3131
this.httpService.post(this.apiUrl, payload, {

cortex-js/src/domain/config/config.interface.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ export interface Config {
22
dataFolderPath: string;
33
cortexCppHost: string;
44
cortexCppPort: number;
5+
// todo: will remove optional when all command request api server
6+
apiServerPort?: number;
7+
apiServerHost?: string;
58
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { CommandRunner } from 'nest-commander';
2+
import { Injectable } from '@nestjs/common';
3+
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
4+
import ora from 'ora';
5+
6+
@Injectable()
7+
export abstract class BaseCommand extends CommandRunner {
8+
constructor(readonly cortexUseCases: CortexUsecases) {
9+
super();
10+
}
11+
protected abstract runCommand(
12+
passedParam: string[],
13+
options?: Record<string, any>,
14+
): Promise<void>;
15+
16+
async run(
17+
passedParam: string[],
18+
options?: Record<string, any>,
19+
): Promise<void> {
20+
const checkingSpinner = ora('Checking API server online...').start();
21+
const result = await this.cortexUseCases.isAPIServerOnline();
22+
if (!result) {
23+
checkingSpinner.fail('API server is offline');
24+
process.exit(1);
25+
}
26+
checkingSpinner.succeed('API server is online');
27+
await this.runCommand(passedParam, options);
28+
}
29+
}

cortex-js/src/infrastructure/commanders/benchmark.command.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
BenchmarkConfig,
55
ParametersConfig,
66
} from './types/benchmark-config.interface';
7+
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
8+
import { BaseCommand } from './base.command';
79

810
@SubCommand({
911
name: 'benchmark',
@@ -14,12 +16,15 @@ import {
1416
description:
1517
'Benchmark and analyze the performance of a specific AI model using a variety of system resources',
1618
})
17-
export class BenchmarkCommand extends CommandRunner {
18-
constructor(private readonly benchmarkUsecases: BenchmarkCliUsecases) {
19-
super();
19+
export class BenchmarkCommand extends BaseCommand {
20+
constructor(
21+
private readonly benchmarkUsecases: BenchmarkCliUsecases,
22+
readonly cortexUsecases: CortexUsecases,
23+
) {
24+
super(cortexUsecases);
2025
}
2126

22-
async run(
27+
async runCommand(
2328
passedParams: string[],
2429
options?: Partial<BenchmarkConfig>,
2530
): Promise<void> {

cortex-js/src/infrastructure/commanders/chat.command.ts

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
CommandRunner,
3-
SubCommand,
4-
Option,
5-
InquirerService,
6-
} from 'nest-commander';
7-
import ora from 'ora';
1+
import { existsSync } from 'fs';
2+
import { SubCommand, Option, InquirerService } from 'nest-commander';
83
import { ChatCliUsecases } from './usecases/chat.cli.usecases';
94
import { exit } from 'node:process';
105
import { PSCliUsecases } from './usecases/ps.cli.usecases';
@@ -17,6 +12,14 @@ import {
1712
TelemetrySource,
1813
} from '@/domain/telemetry/telemetry.interface';
1914
import { ContextService } from '../services/context/context.service';
15+
import { BaseCommand } from './base.command';
16+
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
17+
import { ModelsCliUsecases } from './usecases/models.cli.usecases';
18+
import { Engines } from './types/engine.interface';
19+
import { join } from 'path';
20+
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';
21+
import { FileManagerService } from '../services/file-manager/file-manager.service';
22+
import { isLocalModel } from '@/utils/normalize-model-id';
2023

2124
type ChatOptions = {
2225
threadId?: string;
@@ -35,19 +38,26 @@ type ChatOptions = {
3538
},
3639
})
3740
@SetCommandContext()
38-
export class ChatCommand extends CommandRunner {
41+
export class ChatCommand extends BaseCommand {
3942
constructor(
4043
private readonly inquirerService: InquirerService,
4144
private readonly chatCliUsecases: ChatCliUsecases,
4245
private readonly modelsUsecases: ModelsUsecases,
4346
private readonly psCliUsecases: PSCliUsecases,
4447
readonly contextService: ContextService,
4548
private readonly telemetryUsecases: TelemetryUsecases,
49+
readonly cortexUsecases: CortexUsecases,
50+
readonly modelsCliUsecases: ModelsCliUsecases,
51+
private readonly fileService: FileManagerService,
52+
private readonly initUsecases: EnginesUsecases,
4653
) {
47-
super();
54+
super(cortexUsecases);
4855
}
4956

50-
async run(passedParams: string[], options: ChatOptions): Promise<void> {
57+
async runCommand(
58+
passedParams: string[],
59+
options: ChatOptions,
60+
): Promise<void> {
5161
let modelId = passedParams[0];
5262
// First attempt to get message from input or options
5363
// Extract input from 1 to end of array
@@ -59,7 +69,7 @@ export class ChatCommand extends CommandRunner {
5969
// first input might be message input
6070
message = passedParams.length
6171
? passedParams.join(' ')
62-
: options.message ?? '';
72+
: (options.message ?? '');
6373
// If model ID is not provided, prompt user to select from running models
6474
const models = await this.psCliUsecases.getModels();
6575
if (models.length === 1) {
@@ -71,14 +81,20 @@ export class ChatCommand extends CommandRunner {
7181
}
7282
}
7383

84+
const existingModel = await this.modelsCliUsecases.getModel(modelId);
85+
if (!existingModel || !isLocalModel(existingModel.files)) {
86+
process.exit(1);
87+
}
88+
89+
const engine = existingModel.engine || Engines.llamaCPP;
90+
// Pull engine if not exist
91+
if (
92+
!existsSync(join(await this.fileService.getCortexCppEnginePath(), engine))
93+
) {
94+
await this.initUsecases.installEngine(undefined, 'latest', engine);
95+
}
96+
7497
if (!message) options.attach = true;
75-
const result = await this.chatCliUsecases.chat(
76-
modelId,
77-
options.threadId,
78-
message, // Accept both message from inputs or arguments
79-
options.attach,
80-
false, // Do not stop cortex session or loaded model
81-
);
8298
this.telemetryUsecases.sendEvent(
8399
[
84100
{
@@ -88,7 +104,18 @@ export class ChatCommand extends CommandRunner {
88104
],
89105
TelemetrySource.CLI,
90106
);
91-
return result;
107+
return this.cortexUsecases
108+
.startCortex()
109+
.then(() => this.modelsCliUsecases.startModel(modelId))
110+
.then(() =>
111+
this.chatCliUsecases.chat(
112+
modelId,
113+
options.threadId,
114+
message, // Accept both message from inputs or arguments
115+
options.attach,
116+
false, // Do not stop cortex session or loaded model
117+
),
118+
);
92119
}
93120

94121
modelInquiry = async (models: ModelStat[]) => {
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
import { CommandRunner, SubCommand } from 'nest-commander';
1+
import { SubCommand } from 'nest-commander';
22
import { SetCommandContext } from './decorators/CommandContext';
33
import { ContextService } from '@/infrastructure/services/context/context.service';
44
import { ConfigsGetCommand } from './configs/configs-get.command';
55
import { ConfigsListCommand } from './configs/configs-list.command';
66
import { ConfigsSetCommand } from './configs/configs-set.command';
7+
import { BaseCommand } from './base.command';
8+
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
79

810
@SubCommand({
911
name: 'configs',
1012
description: 'Get cortex configurations',
1113
subCommands: [ConfigsGetCommand, ConfigsListCommand, ConfigsSetCommand],
1214
})
1315
@SetCommandContext()
14-
export class ConfigsCommand extends CommandRunner {
15-
constructor(readonly contextService: ContextService) {
16-
super();
16+
export class ConfigsCommand extends BaseCommand {
17+
constructor(
18+
readonly contextService: ContextService,
19+
readonly cortexUseCases: CortexUsecases,
20+
) {
21+
super(cortexUseCases);
1722
}
1823

19-
async run(): Promise<void> {
24+
async runCommand(): Promise<void> {
2025
this.command?.help();
2126
}
2227
}

cortex-js/src/infrastructure/commanders/configs/configs-get.command.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { CommandRunner, SubCommand } from 'nest-commander';
22
import { SetCommandContext } from '../decorators/CommandContext';
33
import { ContextService } from '@/infrastructure/services/context/context.service';
44
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
5+
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
6+
import { BaseCommand } from '../base.command';
57

68
@SubCommand({
79
name: 'get',
@@ -12,15 +14,16 @@ import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
1214
},
1315
})
1416
@SetCommandContext()
15-
export class ConfigsGetCommand extends CommandRunner {
17+
export class ConfigsGetCommand extends BaseCommand {
1618
constructor(
1719
private readonly configsUsecases: ConfigsUsecases,
1820
readonly contextService: ContextService,
21+
readonly cortexUsecases: CortexUsecases,
1922
) {
20-
super();
23+
super(cortexUsecases);
2124
}
2225

23-
async run(passedParams: string[]): Promise<void> {
26+
async runCommand(passedParams: string[]): Promise<void> {
2427
return this.configsUsecases
2528
.getGroupConfigs(passedParams[0])
2629
.then(console.table);
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
import { CommandRunner, SubCommand } from 'nest-commander';
1+
import { SubCommand } from 'nest-commander';
22
import { SetCommandContext } from '../decorators/CommandContext';
33
import { ContextService } from '@/infrastructure/services/context/context.service';
44
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
5+
import { BaseCommand } from '../base.command';
6+
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
57

68
@SubCommand({
79
name: 'list',
810
description: 'Get all cortex configurations',
911
})
1012
@SetCommandContext()
11-
export class ConfigsListCommand extends CommandRunner {
13+
export class ConfigsListCommand extends BaseCommand {
1214
constructor(
1315
private readonly configsUsecases: ConfigsUsecases,
1416
readonly contextService: ContextService,
17+
readonly cortexUsecases: CortexUsecases,
1518
) {
16-
super();
19+
super(cortexUsecases);
1720
}
1821

19-
async run(): Promise<void> {
22+
async runCommand(): Promise<void> {
2023
return this.configsUsecases.getConfigs().then(console.table);
2124
}
2225
}

cortex-js/src/infrastructure/commanders/configs/configs-set.command.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { CommandRunner, SubCommand, Option } from 'nest-commander';
22
import { SetCommandContext } from '../decorators/CommandContext';
33
import { ContextService } from '@/infrastructure/services/context/context.service';
44
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
5+
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
6+
import { BaseCommand } from '../base.command';
57

68
interface ConfigsSetOption {
79
key: string;
@@ -14,15 +16,19 @@ interface ConfigsSetOption {
1416
description: 'Set a cortex configuration',
1517
})
1618
@SetCommandContext()
17-
export class ConfigsSetCommand extends CommandRunner {
19+
export class ConfigsSetCommand extends BaseCommand {
1820
constructor(
1921
private readonly configsUsecases: ConfigsUsecases,
2022
readonly contextService: ContextService,
23+
readonly cortexUsecases: CortexUsecases,
2124
) {
22-
super();
25+
super(cortexUsecases);
2326
}
2427

25-
async run(passedParams: string[], options: ConfigsSetOption): Promise<void> {
28+
async runCommand(
29+
passedParams: string[],
30+
options: ConfigsSetOption,
31+
): Promise<void> {
2632
return this.configsUsecases
2733
.saveConfig(options.key, options.value, options.group)
2834
.then(() => console.log('Set configuration successfully'));

0 commit comments

Comments
 (0)