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

Commit 91a9678

Browse files
authored
Merge pull request #1333 from janhq/j/some-refactoring
Add support download model for author:model
2 parents c9a44ae + 5930e88 commit 91a9678

26 files changed

+498
-569
lines changed

engine/commands/engine_get_cmd.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ namespace commands {
88

99
void EngineGetCmd::Exec(const std::string& engine_name) const {
1010
auto engine = engine_service_.GetEngineInfo(engine_name);
11-
if (engine == std::nullopt) {
12-
CLI_LOG("Engine " + engine_name + " is not supported!");
11+
if (engine.has_error()) {
12+
CLI_LOG(engine.error());
1313
return;
1414
}
1515

16+
auto version = engine->version.value_or("");
17+
auto variant = engine->variant.value_or("");
1618
tabulate::Table table;
17-
table.add_row({"Name", "Supported Formats", "Version", "Status"});
19+
table.add_row({"Name", "Supported Formats", "Version", "Variant", "Status"});
1820
table.add_row(
19-
{engine->product_name, engine->format, engine->version, engine->status});
21+
{engine->product_name, engine->format, version, variant, engine->status});
2022
std::cout << table << std::endl;
2123
}
2224
}; // namespace commands

engine/commands/engine_list_cmd.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ bool EngineListCmd::Exec() {
99
auto status_list = engine_service.GetEngineInfoList();
1010

1111
tabulate::Table table;
12-
table.add_row({"#", "Name", "Supported Formats", "Version", "Status"});
12+
table.add_row(
13+
{"#", "Name", "Supported Formats", "Version", "Variant", "Status"});
1314
for (int i = 0; i < status_list.size(); i++) {
14-
auto status = status_list[i];
15+
auto engine_status = status_list[i];
1516
std::string index = std::to_string(i + 1);
16-
table.add_row({index, status.product_name, status.format, status.version,
17-
status.status});
17+
auto variant = engine_status.variant.value_or("");
18+
auto version = engine_status.version.value_or("");
19+
table.add_row({index, engine_status.product_name, engine_status.format,
20+
version, variant, engine_status.status});
1821
}
1922

2023
std::cout << table << std::endl;

engine/commands/engine_uninstall_cmd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void EngineUninstallCmd::Exec(const std::string& engine) {
1010
if (result.has_error()) {
1111
CLI_LOG(result.error());
1212
} else {
13-
CLI_LOG("Engine uninstalled successfully");
13+
CLI_LOG("Engine " + engine + " uninstalled successfully!");
1414
}
1515
}
1616
}; // namespace commands

engine/commands/model_del_cmd.cc

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,13 @@
11
#include "model_del_cmd.h"
2-
#include "cmd_info.h"
3-
#include "config/yaml_config.h"
4-
#include "utils/file_manager_utils.h"
5-
#include "utils/modellist_utils.h"
2+
#include "utils/logging_utils.h"
63

74
namespace commands {
8-
bool ModelDelCmd::Exec(const std::string& model_handle) {
9-
modellist_utils::ModelListUtils modellist_handler;
10-
config::YamlHandler yaml_handler;
11-
12-
try {
13-
auto model_entry = modellist_handler.GetModelInfo(model_handle);
14-
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
15-
auto mc = yaml_handler.GetModelConfig();
16-
// Remove yaml file
17-
std::filesystem::remove(model_entry.path_to_model_yaml);
18-
// Remove model files if they are not imported locally
19-
if (model_entry.branch_name != "imported") {
20-
if (mc.files.size() > 0) {
21-
if (mc.engine == "cortex.llamacpp") {
22-
for (auto& file : mc.files) {
23-
std::filesystem::path gguf_p(file);
24-
std::filesystem::remove(gguf_p);
25-
}
26-
} else {
27-
std::filesystem::path f(mc.files[0]);
28-
std::filesystem::remove_all(f);
29-
}
30-
} else {
31-
CTL_WRN("model config files are empty!");
32-
}
33-
}
34-
35-
// update model.list
36-
if (modellist_handler.DeleteModelEntry(model_handle)) {
37-
CLI_LOG("The model " << model_handle << " was deleted");
38-
return true;
39-
} else {
40-
CTL_ERR("Could not delete model: " << model_handle);
41-
return false;
42-
}
43-
} catch (const std::exception& e) {
44-
CLI_LOG("Fail to delete model with ID '" + model_handle + "': " + e.what());
45-
false;
5+
void ModelDelCmd::Exec(const std::string& model_handle) {
6+
auto result = model_service_.DeleteModel(model_handle);
7+
if (result.has_error()) {
8+
CLI_LOG(result.error());
9+
} else {
10+
CLI_LOG("Model " + model_handle + " deleted successfully");
4611
}
4712
}
48-
} // namespace commands
13+
} // namespace commands

engine/commands/model_del_cmd.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#pragma once
22

33
#include <string>
4+
#include "services/model_service.h"
45

56
namespace commands {
67

78
class ModelDelCmd {
89
public:
9-
bool Exec(const std::string& model_handle);
10+
explicit ModelDelCmd() : model_service_{ModelService()} {};
11+
12+
void Exec(const std::string& model_handle);
13+
14+
private:
15+
ModelService model_service_;
1016
};
11-
}
17+
} // namespace commands

engine/commands/model_list_cmd.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "model_list_cmd.h"
2-
#include <filesystem>
32
#include <iostream>
43
#include <tabulate/table.hpp>
54
#include <vector>

engine/commands/model_pull_cmd.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#include "model_pull_cmd.h"
2-
#include "utils/cortexso_parser.h"
2+
#include "utils/logging_utils.h"
33

44
namespace commands {
55
void ModelPullCmd::Exec(const std::string& input) {
6-
model_service_.DownloadModel(input);
6+
auto result = model_service_.DownloadModel(input);
7+
if (result.has_error()) {
8+
CLI_LOG(result.error());
9+
} else {
10+
CLI_LOG("Model downloaded successfully!");
11+
}
712
}
813
}; // namespace commands

engine/commands/run_cmd.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ void RunCmd::Exec() {
1919
// Download model if it does not exist
2020
{
2121
if (!modellist_handler.HasModel(model_handle_)) {
22-
model_id = model_service_.DownloadModel(model_handle_);
23-
if (!model_id.has_value()) {
24-
CTL_ERR("Error: Could not get model_id from handle: " << model_handle_);
22+
auto result = model_service_.DownloadModel(model_handle_);
23+
if (result.has_error()) {
24+
CTL_ERR("Error: " << result.error());
2525
return;
26-
} else {
27-
CTL_INF("model_id: " << model_id.value());
2826
}
27+
CTL_INF("model_id: " << model_id.value());
2928
}
3029
}
3130

engine/controllers/command_line_parser.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,13 @@ void CommandLineParser::SetupModelCommands() {
233233
" models delete [model_id]");
234234
model_del_cmd->group(kSubcommands);
235235
model_del_cmd->add_option("model_id", cml_data_.model_id, "");
236-
model_del_cmd->callback([this, model_del_cmd]() {
236+
model_del_cmd->callback([&]() {
237237
if (cml_data_.model_id.empty()) {
238238
CLI_LOG("[model_id] is required\n");
239239
CLI_LOG(model_del_cmd->help());
240240
return;
241241
};
242-
commands::ModelDelCmd mdc;
243-
mdc.Exec(cml_data_.model_id);
242+
commands::ModelDelCmd().Exec(cml_data_.model_id);
244243
});
245244

246245
std::string model_alias;
@@ -518,4 +517,4 @@ void CommandLineParser::ModelUpdate(CLI::App* parent) {
518517
commands::ModelUpdCmd command(cml_data_.model_id);
519518
command.Exec(cml_data_.model_update_options);
520519
});
521-
}
520+
}

0 commit comments

Comments
 (0)