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

Commit 9c68747

Browse files
committed
chore: check model not exist before pulling
1 parent dc1c170 commit 9c68747

File tree

5 files changed

+56
-20
lines changed

5 files changed

+56
-20
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export class ModelGetCommand extends CommandRunner {
1414
exit(1);
1515
}
1616

17-
const models = await this.modelsCliUsecases.getModel(input[0]);
18-
console.log(models);
17+
const model = await this.modelsCliUsecases.getModel(input[0]);
18+
if (!model) console.error('Model not found');
19+
else console.log(model);
1920
}
2021
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CommandRunner, InquirerService, SubCommand } from 'nest-commander';
22
import { exit } from 'node:process';
33
import { ModelsCliUsecases } from '../usecases/models.cli.usecases';
44
import { RepoDesignation, listFiles } from '@huggingface/hub';
5+
import { ModelNotFoundException } from '@/infrastructure/exception/model-not-found.exception';
56

67
@SubCommand({
78
name: 'pull',
@@ -28,9 +29,16 @@ export class ModelPullCommand extends CommandRunner {
2829
? undefined
2930
: await this.tryToGetBranches(input[0]);
3031

31-
await this.modelsCliUsecases.pullModel(
32-
!branches ? input[0] : await this.handleJanHqModel(input[0], branches),
33-
);
32+
await this.modelsCliUsecases
33+
.pullModel(
34+
!branches ? input[0] : await this.handleJanHqModel(input[0], branches),
35+
)
36+
.catch((e: Error) => {
37+
if (e instanceof ModelNotFoundException)
38+
console.error('Model does not exist.');
39+
else console.error(e);
40+
exit(1);
41+
});
3442

3543
console.log('\nDownload complete!');
3644
exit(0);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ export class ModelStartCommand extends CommandRunner {
3333
}
3434
}
3535

36+
const existingModel = await this.modelsCliUsecases.getModel(modelId);
37+
if (
38+
!existingModel ||
39+
!Array.isArray(existingModel.files) ||
40+
/^(http|https):\/\/[^/]+\/.*/.test(existingModel.files[0])
41+
) {
42+
console.error('Model is not available. Please pull the model first.');
43+
process.exit(1);
44+
}
45+
3646
await this.cortexUsecases
3747
.startCortex(options.attach)
3848
.then(() => this.modelsCliUsecases.startModel(modelId, options.preset))
@@ -41,7 +51,11 @@ export class ModelStartCommand extends CommandRunner {
4151
}
4252

4353
modelInquiry = async () => {
44-
const models = await this.modelsCliUsecases.listAllModels();
54+
const models = (await this.modelsCliUsecases.listAllModels()).filter(
55+
(model) =>
56+
Array.isArray(model.files) &&
57+
!/^(http|https):\/\/[^/]+\/.*/.test(model.files[0]),
58+
);
4559
if (!models.length) throw 'No models found';
4660
const { model } = await this.inquirerService.inquirer.prompt({
4761
type: 'list',

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ export class RunCommand extends CommandRunner {
6363
}
6464

6565
modelInquiry = async () => {
66-
const models = await this.modelsCliUsecases.listAllModels();
66+
const models = (await this.modelsCliUsecases.listAllModels()).filter(
67+
(model) =>
68+
Array.isArray(model.files) &&
69+
!/^(http|https):\/\/[^/]+\/.*/.test(model.files[0]),
70+
);
6771
if (!models.length) throw 'No models found';
6872
const { model } = await this.inquirerService.inquirer.prompt({
6973
type: 'list',

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class ModelsCliUsecases {
105105
* @param modelId
106106
* @returns
107107
*/
108-
private async getModelOrStop(modelId: string): Promise<Model> {
108+
async getModelOrStop(modelId: string): Promise<Model> {
109109
const model = await this.modelsUsecases.findOne(modelId);
110110
if (!model) {
111111
console.debug('Model not found');
@@ -127,9 +127,8 @@ export class ModelsCliUsecases {
127127
* @param modelId
128128
* @returns
129129
*/
130-
async getModel(modelId: string): Promise<Model> {
131-
const model = await this.getModelOrStop(modelId);
132-
return model;
130+
async getModel(modelId: string): Promise<Model | null> {
131+
return this.modelsUsecases.findOne(modelId);
133132
}
134133

135134
/**
@@ -147,7 +146,12 @@ export class ModelsCliUsecases {
147146
* @param modelId
148147
*/
149148
async pullModel(modelId: string) {
150-
if (await this.modelsUsecases.findOne(modelId)) {
149+
const existingModel = await this.modelsUsecases.findOne(modelId);
150+
if (
151+
existingModel &&
152+
Array.isArray(existingModel.files) &&
153+
!/^(http|https):\/\/[^/]+\/.*/.test(existingModel.files[0])
154+
) {
151155
console.error('Model already exists');
152156
process.exit(1);
153157
}
@@ -161,15 +165,20 @@ export class ModelsCliUsecases {
161165
bar.update(progress);
162166
};
163167

164-
await this.modelsUsecases.downloadModel(modelId, callback);
168+
try {
169+
await this.modelsUsecases.downloadModel(modelId, callback);
165170

166-
const model = await this.modelsUsecases.findOne(modelId);
167-
const fileUrl = join(
168-
await this.fileService.getModelsPath(),
169-
normalizeModelId(modelId),
170-
basename((model?.files as string[])[0]),
171-
);
172-
await this.modelsUsecases.update(modelId, { files: [fileUrl] });
171+
const model = await this.modelsUsecases.findOne(modelId);
172+
const fileUrl = join(
173+
await this.fileService.getModelsPath(),
174+
normalizeModelId(modelId),
175+
basename((model?.files as string[])[0]),
176+
);
177+
await this.modelsUsecases.update(modelId, { files: [fileUrl] });
178+
} catch (err) {
179+
bar.stop();
180+
throw err;
181+
}
173182
}
174183

175184
private async getHFModelTokenizer(

0 commit comments

Comments
 (0)