|
1 | | -import { CommandRunner, SubCommand } from 'nest-commander'; |
| 1 | +import { SubCommand } from 'nest-commander'; |
2 | 2 | import { ModelStartCommand } from './models/model-start.command'; |
3 | 3 | import { ModelGetCommand } from './models/model-get.command'; |
4 | 4 | import { ModelListCommand } from './models/model-list.command'; |
5 | 5 | import { ModelStopCommand } from './models/model-stop.command'; |
6 | | -import { ModelPullCommand } from './models/model-pull.command'; |
7 | 6 | import { ModelRemoveCommand } from './models/model-remove.command'; |
8 | 7 | import { ModelUpdateCommand } from './models/model-update.command'; |
9 | | -import { RunCommand } from './shortcuts/run.command'; |
10 | 8 | import { BaseCommand } from './base.command'; |
| 9 | +import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; |
| 10 | +import { ModuleRef } from '@nestjs/core'; |
| 11 | +import { ModelPullCommand } from './models/model-pull.command'; |
11 | 12 |
|
12 | 13 | @SubCommand({ |
13 | 14 | name: 'models', |
14 | 15 | subCommands: [ |
15 | | - ModelPullCommand, |
16 | 16 | ModelStartCommand, |
17 | 17 | ModelStopCommand, |
18 | 18 | ModelListCommand, |
19 | 19 | ModelGetCommand, |
20 | 20 | ModelRemoveCommand, |
21 | 21 | ModelUpdateCommand, |
22 | | - RunCommand, |
23 | 22 | ], |
24 | 23 | description: 'Subcommands for managing models', |
25 | 24 | }) |
26 | 25 | export class ModelsCommand extends BaseCommand { |
27 | | - async runCommand(): Promise<void> { |
28 | | - this.command?.help(); |
| 26 | + constructor( |
| 27 | + private readonly moduleRef: ModuleRef, |
| 28 | + readonly cortexUsecases: CortexUsecases, |
| 29 | + ) { |
| 30 | + super(cortexUsecases); |
| 31 | + } |
| 32 | + |
| 33 | + commandMap: { [key: string]: any } = { |
| 34 | + pull: ModelPullCommand, |
| 35 | + start: ModelStartCommand, |
| 36 | + stop: ModelStopCommand, |
| 37 | + list: ModelListCommand, |
| 38 | + get: ModelGetCommand, |
| 39 | + remove: ModelRemoveCommand, |
| 40 | + update: ModelUpdateCommand, |
| 41 | + }; |
| 42 | + async runCommand(passedParam: string[], options: any) { |
| 43 | + const [parameter, command, ...restParams] = passedParam; |
| 44 | + if (command !== 'list' && !parameter) { |
| 45 | + console.error('Model id is required.'); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // Handle the commands accordingly |
| 50 | + const commandClass = this.commandMap[command as string]; |
| 51 | + if (!commandClass) { |
| 52 | + this.command?.help(); |
| 53 | + return; |
| 54 | + } |
| 55 | + const modelId = parameter; |
| 56 | + await this.runModelCommand( |
| 57 | + commandClass, |
| 58 | + [modelId, ...(restParams || [])], |
| 59 | + options, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + private async runModelCommand( |
| 64 | + commandClass: any, |
| 65 | + params: string[] = [], |
| 66 | + options?: any, |
| 67 | + ) { |
| 68 | + const commandInstance = this.moduleRef.get(commandClass, { strict: false }); |
| 69 | + if (commandInstance) { |
| 70 | + await commandInstance.run(params, options); |
| 71 | + } else { |
| 72 | + console.error('Command not found.'); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + help() { |
| 77 | + console.log('Usage: cortex models <subcommand|parameter> [subcommand]'); |
| 78 | + console.log('Commands:'); |
| 79 | + console.log(' <model_id> start - Start a model by ID'); |
| 80 | + console.log(' <model_id> stop - Stop a model by ID'); |
| 81 | + console.log(' list - List all available models'); |
| 82 | + console.log(' <model_id> get - Get model details by ID'); |
| 83 | + console.log(' <model_id> remove - Remove a model by ID'); |
| 84 | + console.log(' <model_id> update - Update a model by ID'); |
29 | 85 | } |
30 | 86 | } |
0 commit comments