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

Commit d87bbe2

Browse files
authored
feat: support cortex engines init command (#815)
1 parent 683b12c commit d87bbe2

File tree

14 files changed

+92
-25
lines changed

14 files changed

+92
-25
lines changed

cortex-js/src/command.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { ConfigsListCommand } from './infrastructure/commanders/configs/configs-
4444
import { ConfigsSetCommand } from './infrastructure/commanders/configs/configs-set.command';
4545
import { EnginesListCommand } from './infrastructure/commanders/engines/engines-list.command';
4646
import { EnginesGetCommand } from './infrastructure/commanders/engines/engines-get.command';
47+
import { EnginesInitCommand } from './infrastructure/commanders/engines/engines-init.command';
4748

4849
@Module({
4950
imports: [
@@ -112,6 +113,7 @@ import { EnginesGetCommand } from './infrastructure/commanders/engines/engines-g
112113
// Engines
113114
EnginesListCommand,
114115
EnginesGetCommand,
116+
EnginesInitCommand,
115117
],
116118
})
117119
export class CommandModule {}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { CommandRunner, SubCommand } from 'nest-commander';
22
import { SetCommandContext } from './decorators/CommandContext';
33
import { ContextService } from '@/infrastructure/services/context/context.service';
4-
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';
54
import { EnginesListCommand } from './engines/engines-list.command';
65
import { EnginesGetCommand } from './engines/engines-get.command';
6+
import { EnginesInitCommand } from './engines/engines-init.command';
77

88
@SubCommand({
99
name: 'engines',
10-
subCommands: [EnginesListCommand, EnginesGetCommand],
10+
subCommands: [EnginesListCommand, EnginesGetCommand, EnginesInitCommand],
1111
description: 'Get cortex engines',
1212
})
1313
@SetCommandContext()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { CommandRunner, SubCommand } from 'nest-commander';
2+
import { SetCommandContext } from '../decorators/CommandContext';
3+
import { ContextService } from '@/infrastructure/services/context/context.service';
4+
import { InitCliUsecases } from '../usecases/init.cli.usecases';
5+
import { Engines } from '../types/engine.interface';
6+
7+
@SubCommand({
8+
name: 'init',
9+
description: 'Setup engine',
10+
arguments: '<name>',
11+
argsDescription: {
12+
name: 'Engine name to setup',
13+
},
14+
})
15+
@SetCommandContext()
16+
export class EnginesInitCommand extends CommandRunner {
17+
constructor(
18+
private readonly initUsecases: InitCliUsecases,
19+
readonly contextService: ContextService,
20+
) {
21+
super();
22+
}
23+
24+
async run(passedParams: string[]): Promise<void> {
25+
const engine = passedParams[0];
26+
const options = passedParams.includes(Engines.llamaCPP)
27+
? await this.initUsecases.defaultInstallationOptions()
28+
: {};
29+
return this.initUsecases
30+
.installEngine(
31+
options,
32+
engine.includes('@') ? engine.split('@')[1] : 'latest',
33+
engine,
34+
true
35+
)
36+
.then(() => console.log('Engine installed successfully!'))
37+
.catch(() => console.error('Engine not found or installation failed!'));
38+
}
39+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ import { ContextService } from '../services/context/context.service';
1010
@SetCommandContext()
1111
export class KillCommand extends CommandRunner {
1212
constructor(
13-
private readonly usecases: CortexUsecases,
13+
private readonly cortexUsecases: CortexUsecases,
1414
readonly contextService: ContextService,
1515
) {
1616
super();
1717
}
1818
async run(): Promise<void> {
19-
return this.usecases.stopCortex().then(console.log);
19+
return this.cortexUsecases
20+
.stopCortex()
21+
.then(this.cortexUsecases.stopServe)
22+
.then(() => console.log('Cortex processes stopped successfully!'));
2023
}
2124
}

cortex-js/src/infrastructure/commanders/models/model-pull.command.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { join } from 'node:path';
1414
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
1515
import { InitCliUsecases } from '../usecases/init.cli.usecases';
1616
import { checkModelCompatibility } from '@/utils/model-check';
17+
import { Engines } from '../types/engine.interface';
1718

1819
@SubCommand({
1920
name: 'pull',
@@ -52,7 +53,7 @@ export class ModelPullCommand extends CommandRunner {
5253
});
5354

5455
const existingModel = await this.modelsCliUsecases.getModel(modelId);
55-
const engine = existingModel?.engine || 'cortex.llamacpp';
56+
const engine = existingModel?.engine || Engines.llamaCPP;
5657

5758
// Pull engine if not exist
5859
if (

cortex-js/src/infrastructure/commanders/models/model-start.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class ModelStartCommand extends CommandRunner {
6767

6868
checkModelCompatibility(modelId);
6969

70-
const engine = existingModel.engine || 'cortex.llamacpp';
70+
const engine = existingModel.engine || Engines.llamaCPP;
7171
// Pull engine if not exist
7272
if (
7373
!existsSync(join(await this.fileService.getCortexCppEnginePath(), engine))

cortex-js/src/infrastructure/commanders/shortcuts/run.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class RunCommand extends CommandRunner {
8080

8181
checkModelCompatibility(modelId);
8282

83-
const engine = existingModel.engine || 'cortex.llamacpp';
83+
const engine = existingModel.engine || Engines.llamaCPP;
8484
// Pull engine if not exist
8585
if (
8686
!existsSync(join(await this.fileService.getCortexCppEnginePath(), engine))

cortex-js/src/infrastructure/commanders/test/helpers.command.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ describe('Helper commands', () => {
9898
await CommandTestFactory.run(commandInstance, ['kill']);
9999
await CommandTestFactory.run(commandInstance, ['ps']);
100100

101-
expect(logMock.firstCall?.args[0]).toEqual({
102-
message: 'Cortex stopped successfully',
103-
status: 'success',
104-
});
101+
expect(logMock.firstCall?.args[0]).toEqual("Cortex processes stopped successfully!");
105102
expect(tableMock.firstCall?.args[0]).toBeInstanceOf(Array);
106103
expect(tableMock.firstCall?.args[0].length).toEqual(0);
107104
},

cortex-js/src/infrastructure/commanders/usecases/init.cli.usecases.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ export class InitCliUsecases {
6666
!existsSync(
6767
join(
6868
await this.fileManagerService.getCortexCppEnginePath(),
69-
'cortex.llamacpp',
69+
Engines.llamaCPP,
7070
),
71-
)
71+
) ||
72+
(engine === Engines.llamaCPP && force)
7273
)
7374
await this.installLlamaCppEngine(options, version);
7475

75-
if (engine !== 'cortex.llamacpp')
76-
await this.installAcceleratedEngine('latest', engine);
76+
if (engine !== Engines.llamaCPP)
77+
await this.installAcceleratedEngine(version, engine);
7778

7879
configs.initialized = true;
7980
await this.fileManagerService.writeConfigFile(configs);

cortex-js/src/infrastructure/commanders/usecases/ps.cli.usecases.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { HttpService } from '@nestjs/axios';
99
import { firstValueFrom } from 'rxjs';
1010
import { ModelStat } from '@commanders/types/model-stat.interface';
1111
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
12+
import { Engines } from '../types/engine.interface';
1213

1314
interface ModelStatResponse {
1415
object: string;
@@ -47,7 +48,7 @@ export class PSCliUsecases {
4748
currentTime.getTime() - new Date(startTime).getTime();
4849
return {
4950
modelId: e.id,
50-
engine: e.engine ?? 'cortex.llamacpp',
51+
engine: e.engine ?? Engines.llamaCPP,
5152
status: 'running',
5253
duration: this.formatDuration(duration),
5354
ram: e.ram ?? '-',

0 commit comments

Comments
 (0)