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

Commit 8034171

Browse files
authored
chore: Update the run command to allow model selection and add the Cortex version. (#640)
1 parent 69c371b commit 8034171

File tree

4 files changed

+79
-48
lines changed

4 files changed

+79
-48
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RootCommand, CommandRunner } from 'nest-commander';
1+
import { RootCommand, CommandRunner, Option } from 'nest-commander';
22
import { ServeCommand } from './serve.command';
33
import { ChatCommand } from './chat.command';
44
import { ModelsCommand } from './models.command';
@@ -7,7 +7,11 @@ import { RunCommand } from './shortcuts/run.command';
77
import { ModelPullCommand } from './models/model-pull.command';
88
import { PSCommand } from './ps.command';
99
import { KillCommand } from './kill.command';
10+
import pkg from '@/../package.json';
1011

12+
interface CortexCommandOptions {
13+
version: boolean;
14+
}
1115
@RootCommand({
1216
subCommands: [
1317
ModelsCommand,
@@ -22,5 +26,17 @@ import { KillCommand } from './kill.command';
2226
description: 'Cortex CLI',
2327
})
2428
export class CortexCommand extends CommandRunner {
25-
async run(): Promise<void> {}
29+
async run(input: string[], option: CortexCommandOptions): Promise<void> {
30+
if (option.version) console.log(pkg.version);
31+
}
32+
33+
@Option({
34+
flags: '-v, --version',
35+
description: 'Cortex version',
36+
defaultValue: false,
37+
name: 'version',
38+
})
39+
parseVersion() {
40+
return true;
41+
}
2642
}

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

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

3535
await this.cortexUsecases
3636
.startCortex(options.attach)
37-
.then(() => this.modelsCliUsecases.startModel(input[0]))
37+
.then(() => this.modelsCliUsecases.startModel(modelId))
3838
.then(console.log)
3939
.then(() => !options.attach && process.exit(0));
4040
}
Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
2-
import { ModelsUsecases } from '@/usecases/models/models.usecases';
3-
import { CommandRunner, SubCommand, Option } from 'nest-commander';
2+
import {
3+
CommandRunner,
4+
SubCommand,
5+
Option,
6+
InquirerService,
7+
} from 'nest-commander';
48
import { exit } from 'node:process';
59
import { ChatCliUsecases } from '../usecases/chat.cli.usecases';
610
import { defaultCortexCppHost, defaultCortexCppPort } from 'constant';
11+
import { ModelsCliUsecases } from '../usecases/models.cli.usecases';
712

813
type RunOptions = {
914
threadId?: string;
@@ -15,27 +20,29 @@ type RunOptions = {
1520
})
1621
export class RunCommand extends CommandRunner {
1722
constructor(
18-
private readonly modelsUsecases: ModelsUsecases,
23+
private readonly modelsCliUsecases: ModelsCliUsecases,
1924
private readonly cortexUsecases: CortexUsecases,
2025
private readonly chatCliUsecases: ChatCliUsecases,
26+
private readonly inquirerService: InquirerService,
2127
) {
2228
super();
2329
}
2430

2531
async run(input: string[], option?: RunOptions): Promise<void> {
26-
if (input.length === 0) {
27-
console.error('Model Id is required');
28-
exit(1);
32+
let modelId = input[0];
33+
if (!modelId) {
34+
try {
35+
modelId = await this.modelInquiry();
36+
} catch {
37+
console.error('Model ID is required');
38+
exit(1);
39+
}
2940
}
30-
const modelId = input[0];
3141

32-
await this.cortexUsecases.startCortex(
33-
false,
34-
defaultCortexCppHost,
35-
defaultCortexCppPort,
36-
);
37-
await this.modelsUsecases.startModel(modelId);
38-
await this.chatCliUsecases.chat(modelId, option?.threadId);
42+
return this.cortexUsecases
43+
.startCortex(false, defaultCortexCppHost, defaultCortexCppPort)
44+
.then(() => this.modelsCliUsecases.startModel(modelId))
45+
.then(() => this.chatCliUsecases.chat(modelId, option?.threadId));
3946
}
4047

4148
@Option({
@@ -45,4 +52,19 @@ export class RunCommand extends CommandRunner {
4552
parseThreadId(value: string) {
4653
return value;
4754
}
55+
56+
modelInquiry = async () => {
57+
const models = await this.modelsCliUsecases.listAllModels();
58+
if (!models.length) throw 'No models found';
59+
const { model } = await this.inquirerService.inquirer.prompt({
60+
type: 'list',
61+
name: 'model',
62+
message: 'Select a model to start:',
63+
choices: models.map((e) => ({
64+
name: e.name,
65+
value: e.id,
66+
})),
67+
});
68+
return model;
69+
};
4870
}

cortex-js/src/usecases/models/models.usecases.ts

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -141,30 +141,27 @@ export class ModelsUsecases {
141141
if (!engine) {
142142
return {
143143
message: 'No extension handler found for model',
144-
modelId: modelId,
144+
modelId,
145145
};
146146
}
147147

148148
return engine
149149
.loadModel(model, settings)
150-
.then(() => {
151-
return {
152-
message: 'Model loaded successfully',
153-
modelId: modelId,
154-
};
155-
})
156-
.catch((e) => {
157-
if (e.code === AxiosError.ERR_BAD_REQUEST) {
158-
return {
159-
message: 'Model already loaded',
160-
modelId: modelId,
161-
};
162-
}
163-
return {
164-
message: 'Model failed to load',
165-
modelId: modelId,
166-
};
167-
});
150+
.then(() => ({
151+
message: 'Model loaded successfully',
152+
modelId,
153+
}))
154+
.catch((e) =>
155+
e.code === AxiosError.ERR_BAD_REQUEST
156+
? {
157+
message: 'Model already loaded',
158+
modelId,
159+
}
160+
: {
161+
message: 'Model failed to load',
162+
modelId,
163+
},
164+
);
168165
}
169166

170167
async stopModel(modelId: string): Promise<StartModelSuccessDto> {
@@ -183,18 +180,14 @@ export class ModelsUsecases {
183180

184181
return engine
185182
.unloadModel(modelId)
186-
.then(() => {
187-
return {
188-
message: 'Model is stopped',
189-
modelId,
190-
};
191-
})
192-
.catch(() => {
193-
return {
194-
message: 'Failed to stop model',
195-
modelId,
196-
};
197-
});
183+
.then(() => ({
184+
message: 'Model is stopped',
185+
modelId,
186+
}))
187+
.catch(() => ({
188+
message: 'Failed to stop model',
189+
modelId,
190+
}));
198191
}
199192

200193
async downloadModel(modelId: string, callback?: (progress: number) => void) {

0 commit comments

Comments
 (0)