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

Commit 61f6c1b

Browse files
feat: change engine syntax (#889)
1 parent b562ecb commit 61f6c1b

File tree

7 files changed

+71
-14
lines changed

7 files changed

+71
-14
lines changed
Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,56 @@
1+
import { invert } from 'lodash';
12
import { CommandRunner, SubCommand } from 'nest-commander';
23
import { SetCommandContext } from './decorators/CommandContext';
34
import { ContextService } from '@/infrastructure/services/context/context.service';
45
import { EnginesListCommand } from './engines/engines-list.command';
56
import { EnginesGetCommand } from './engines/engines-get.command';
67
import { EnginesInitCommand } from './engines/engines-init.command';
8+
import { ModuleRef } from '@nestjs/core';
9+
import { EngineNamesMap } from './types/engine.interface';
710

811
@SubCommand({
912
name: 'engines',
1013
subCommands: [EnginesListCommand, EnginesGetCommand, EnginesInitCommand],
1114
description: 'Get cortex engines',
15+
arguments: '<command|parameter> [subcommand]',
1216
})
1317
@SetCommandContext()
1418
export class EnginesCommand extends CommandRunner {
15-
constructor(readonly contextService: ContextService) {
19+
commandMap: { [key: string]: any } = {
20+
list: EnginesListCommand,
21+
get: EnginesGetCommand,
22+
init: EnginesInitCommand,
23+
};
24+
25+
constructor(
26+
readonly contextService: ContextService,
27+
private readonly moduleRef: ModuleRef,
28+
) {
1629
super();
1730
}
31+
async run(passedParam: string[]): Promise<void> {
32+
const [parameter, command] = passedParam;
33+
if (command !== 'list' && !parameter) {
34+
console.error('Engine name is required.');
35+
return;
36+
}
37+
38+
// Handle the commands accordingly
39+
const commandClass = this.commandMap[command as string];
40+
if (!commandClass) {
41+
this.command?.help();
42+
return;
43+
}
44+
const engine = invert(EngineNamesMap)[parameter] || parameter;
45+
await this.runCommand(commandClass, [engine]);
46+
}
1847

19-
async run(): Promise<void> {
20-
this.command?.help();
48+
private async runCommand(commandClass: any, params: string[] = []) {
49+
const commandInstance = this.moduleRef.get(commandClass, { strict: false });
50+
if (commandInstance) {
51+
await commandInstance.run(params);
52+
} else {
53+
console.error('Command not found.');
54+
}
2155
}
2256
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { CommandRunner, SubCommand } from 'nest-commander';
22
import { SetCommandContext } from '../decorators/CommandContext';
33
import { ContextService } from '@/infrastructure/services/context/context.service';
44
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';
5+
import { EngineNamesMap, Engines } from '../types/engine.interface';
56

67
@SubCommand({
7-
name: 'get',
8+
name: '<name> get',
89
description: 'Get an engine',
9-
arguments: '<name>',
1010
argsDescription: {
1111
name: 'Engine name to get',
1212
},
@@ -21,6 +21,15 @@ export class EnginesGetCommand extends CommandRunner {
2121
}
2222

2323
async run(passedParams: string[]): Promise<void> {
24-
return this.engineUsecases.getEngine(passedParams[0]).then(console.table);
24+
return this.engineUsecases.getEngine(passedParams[0]).then((engine) => {
25+
if (!engine) {
26+
console.error('Engine not found.');
27+
} else {
28+
console.table({
29+
...engine,
30+
name: EngineNamesMap[engine.name as Engines],
31+
});
32+
}
33+
});
2534
}
2635
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
77
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
88

99
@SubCommand({
10-
name: 'init',
10+
name: '<name> init',
1111
description: 'Setup engine',
12-
arguments: '<name>',
1312
argsDescription: {
1413
name: 'Engine name to setup',
1514
},
@@ -49,7 +48,7 @@ export class EnginesInitCommand extends CommandRunner {
4948
params,
5049
engine.includes('@') ? engine.split('@')[1] : 'latest',
5150
engine,
52-
true
51+
true,
5352
)
5453
.then(() => console.log('Engine installed successfully!'))
5554
.catch((e) =>

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CommandRunner, SubCommand } from 'nest-commander';
22
import { SetCommandContext } from '../decorators/CommandContext';
33
import { ContextService } from '@/infrastructure/services/context/context.service';
44
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';
5+
import { EngineNamesMap, Engines } from '../types/engine.interface';
56

67
@SubCommand({
78
name: 'list',
@@ -17,6 +18,12 @@ export class EnginesListCommand extends CommandRunner {
1718
}
1819

1920
async run(): Promise<void> {
20-
return this.enginesUsecases.getEngines().then(console.table);
21+
return this.enginesUsecases.getEngines().then((engines) => {
22+
const enginesTable = engines.map((engine) => ({
23+
...engine,
24+
name: EngineNamesMap[engine.name as Engines],
25+
}));
26+
console.table(enginesTable);
27+
});
2128
}
2229
}

cortex-js/src/infrastructure/commanders/types/engine.interface.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ export enum Engines {
99
openai = 'openai',
1010
anthropic = 'anthropic',
1111
}
12+
13+
export const EngineNamesMap: {
14+
[key in string]: string;
15+
} = {
16+
[Engines.llamaCPP]: 'llamacpp',
17+
[Engines.onnx]: 'onnx',
18+
[Engines.tensorrtLLM]: 'tensorrt-llm',
19+
};

cortex-js/src/infrastructure/dtos/engines/engines.dto.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ export class EngineDto implements Partial<Extension> {
66
@ApiProperty({
77
type: String,
88
example: 'cortex.llamacpp',
9-
description:
10-
'The name of the engine that you want to retrieve.',
9+
description: 'The name of the engine that you want to retrieve.',
1110
})
1211
@IsString()
1312
name: string;

cortex-js/src/infrastructure/dtos/models/model.dto.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class ModelDto implements Partial<Model> {
2828

2929
@ApiProperty({
3030
type: [String],
31-
example: ["End"],
31+
example: ['End'],
3232
description:
3333
'Defines specific tokens or phrases that signal the model to stop producing further output.',
3434
})
@@ -116,7 +116,8 @@ export class ModelDto implements Partial<Model> {
116116

117117
@ApiProperty({
118118
description: 'The prompt to use for internal configuration',
119-
example: "You are an assistant with expert knowledge in {subject}. Please provide a detailed and accurate response to the following query: {query}. Ensure that your response is clear, concise, and informative."
119+
example:
120+
'You are an assistant with expert knowledge in {subject}. Please provide a detailed and accurate response to the following query: {query}. Ensure that your response is clear, concise, and informative.',
120121
})
121122
@IsOptional()
122123
@IsString()

0 commit comments

Comments
 (0)