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

Commit 03c948b

Browse files
committed
feat: pull engine on model pull or start
1 parent b4a5f0a commit 03c948b

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { InitCliUsecases } from './usecases/init.cli.usecases';
88
import { InitOptions } from './types/init-options.interface';
99
import { SetCommandContext } from './decorators/CommandContext';
10+
import { ContextService } from '../services/context/context.service';
1011

1112
@SubCommand({
1213
name: 'init',
@@ -22,6 +23,7 @@ export class InitCommand extends CommandRunner {
2223
constructor(
2324
private readonly inquirerService: InquirerService,
2425
private readonly initUsecases: InitCliUsecases,
26+
readonly contextService: ContextService,
2527
) {
2628
super();
2729
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export class KillCommand extends CommandRunner {
1616
super();
1717
}
1818
async run(): Promise<void> {
19-
this.usecases.stopCortex().then(console.log);
19+
return this.usecases.stopCortex().then(console.log);
2020
}
2121
}

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { SetCommandContext } from '../decorators/CommandContext';
44
import { ModelsCliUsecases } from '@commanders/usecases/models.cli.usecases';
55
import { ModelNotFoundException } from '@/infrastructure/exception/model-not-found.exception';
66
import { ContextService } from '@/infrastructure/services/context/context.service';
7+
import { existsSync } from 'fs';
8+
import { join } from 'node:path';
9+
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
10+
import { InitCliUsecases } from '../usecases/init.cli.usecases';
711

812
@SubCommand({
913
name: 'pull',
@@ -17,6 +21,8 @@ import { ContextService } from '@/infrastructure/services/context/context.servic
1721
export class ModelPullCommand extends CommandRunner {
1822
constructor(
1923
private readonly modelsCliUsecases: ModelsCliUsecases,
24+
private readonly initUsecases: InitCliUsecases,
25+
private readonly fileService: FileManagerService,
2026
readonly contextService: ContextService,
2127
) {
2228
super();
@@ -27,15 +33,27 @@ export class ModelPullCommand extends CommandRunner {
2733
console.error('Model Id is required');
2834
exit(1);
2935
}
36+
const modelId = passedParams[0];
3037

31-
await this.modelsCliUsecases
32-
.pullModel(passedParams[0])
33-
.catch((e: Error) => {
34-
if (e instanceof ModelNotFoundException)
35-
console.error('Model does not exist.');
36-
else console.error(e);
37-
exit(1);
38-
});
38+
const existingModel = await this.modelsCliUsecases.getModel(modelId);
39+
const engine = existingModel?.engine || 'cortex.llamacpp';
40+
// Pull engine if not exist
41+
if (
42+
!existsSync(
43+
join(await this.fileService.getDataFolderPath(), 'engines', engine),
44+
)
45+
) {
46+
await this.initUsecases.installEngine(
47+
await this.initUsecases.defaultInstallationOptions(),
48+
);
49+
}
50+
51+
await this.modelsCliUsecases.pullModel(modelId).catch((e: Error) => {
52+
if (e instanceof ModelNotFoundException)
53+
console.error('Model does not exist.');
54+
else console.error(e);
55+
exit(1);
56+
});
3957

4058
console.log('\nDownload complete!');
4159
exit(0);

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import { ModelsCliUsecases } from '@commanders/usecases/models.cli.usecases';
99
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
1010
import { SetCommandContext } from '../decorators/CommandContext';
1111
import { ContextService } from '@/infrastructure/services/context/context.service';
12+
import { InitCliUsecases } from '../usecases/init.cli.usecases';
13+
import { existsSync } from 'node:fs';
14+
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
15+
import { join } from 'node:path';
1216

1317
type ModelStartOptions = {
1418
attach: boolean;
@@ -29,6 +33,8 @@ export class ModelStartCommand extends CommandRunner {
2933
private readonly inquirerService: InquirerService,
3034
private readonly cortexUsecases: CortexUsecases,
3135
private readonly modelsCliUsecases: ModelsCliUsecases,
36+
private readonly initUsecases: InitCliUsecases,
37+
private readonly fileService: FileManagerService,
3238
readonly contextService: ContextService,
3339
) {
3440
super();
@@ -54,7 +60,17 @@ export class ModelStartCommand extends CommandRunner {
5460
console.error('Model is not available. Please pull the model first.');
5561
process.exit(1);
5662
}
57-
63+
const engine = existingModel.engine || 'cortex.llamacpp';
64+
// Pull engine if not exist
65+
if (
66+
!existsSync(
67+
join(await this.fileService.getDataFolderPath(), 'engines', engine),
68+
)
69+
) {
70+
await this.initUsecases.installEngine(
71+
await this.initUsecases.defaultInstallationOptions(),
72+
);
73+
}
5874
await this.cortexUsecases
5975
.startCortex(options.attach)
6076
.then(() => this.modelsCliUsecases.startModel(modelId, options.preset))

0 commit comments

Comments
 (0)