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

Commit 23600c9

Browse files
committed
[WIP] some refactoring
1 parent 3320218 commit 23600c9

File tree

12 files changed

+274
-280
lines changed

12 files changed

+274
-280
lines changed

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/controllers/command_line_parser.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ void CommandLineParser::SetupCommonCommands() {
134134
}
135135

136136
commands::ChatCmd().Exec(cml_data_.config.apiServerHost,
137-
std::stoi(cml_data_.config.apiServerPort), cml_data_.model_id,
138-
cml_data_.msg);
137+
std::stoi(cml_data_.config.apiServerPort),
138+
cml_data_.model_id, cml_data_.msg);
139139
});
140140
}
141141

@@ -227,14 +227,13 @@ void CommandLineParser::SetupModelCommands() {
227227
" models delete [model_id]");
228228
model_del_cmd->group(kSubcommands);
229229
model_del_cmd->add_option("model_id", cml_data_.model_id, "");
230-
model_del_cmd->callback([this, model_del_cmd]() {
230+
model_del_cmd->callback([&]() {
231231
if (cml_data_.model_id.empty()) {
232232
CLI_LOG("[model_id] is required\n");
233233
CLI_LOG(model_del_cmd->help());
234234
return;
235235
};
236-
commands::ModelDelCmd mdc;
237-
mdc.Exec(cml_data_.model_id);
236+
commands::ModelDelCmd().Exec(cml_data_.model_id);
238237
});
239238

240239
std::string model_alias;
@@ -512,4 +511,4 @@ void CommandLineParser::ModelUpdate(CLI::App* parent) {
512511
commands::ModelUpdCmd command(cml_data_.model_id);
513512
command.Exec(cml_data_.model_update_options);
514513
});
515-
}
514+
}

engine/controllers/engines.cc

Lines changed: 30 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#include "engines.h"
2-
#include <filesystem>
3-
#include <sstream>
4-
#include <stdexcept>
52
#include <utility>
63
#include "services/engine_service.h"
74
#include "utils/archive_utils.h"
85
#include "utils/cortex_utils.h"
9-
#include "utils/system_info_utils.h"
6+
#include "utils/logging_utils.h"
107

118
void Engines::InstallEngine(
129
const HttpRequestPtr& req,
1310
std::function<void(const HttpResponsePtr&)>&& callback,
14-
const std::string& engine) const {
15-
LOG_DEBUG << "InitEngine, Engine: " << engine;
11+
const std::string& engine) {
12+
1613
if (engine.empty()) {
1714
Json::Value res;
1815
res["message"] = "Engine name is required";
@@ -23,98 +20,27 @@ void Engines::InstallEngine(
2320
return;
2421
}
2522

26-
auto system_info = system_info_utils::GetSystemInfo();
2723
auto version{"latest"};
28-
constexpr auto gitHubHost = "https://api.github.com";
29-
30-
std::ostringstream engineReleasePath;
31-
engineReleasePath << "/repos/janhq/" << engine << "/releases/" << version;
32-
33-
httplib::Client cli(gitHubHost);
34-
using namespace nlohmann;
35-
if (auto res = cli.Get(engineReleasePath.str())) {
36-
if (res->status == httplib::StatusCode::OK_200) {
37-
try {
38-
auto jsonResponse = json::parse(res->body);
39-
auto assets = jsonResponse["assets"];
40-
41-
auto os_arch{system_info->os + "-" + system_info->arch};
42-
for (auto& asset : assets) {
43-
auto assetName = asset["name"].get<std::string>();
44-
if (assetName.find(os_arch) != std::string::npos) {
45-
auto download_url =
46-
asset["browser_download_url"].get<std::string>();
47-
auto name = asset["name"].get<std::string>();
48-
LOG_INFO << "Download url: " << download_url;
49-
50-
std::filesystem::path engine_folder_path =
51-
file_manager_utils::GetContainerFolderPath(
52-
file_manager_utils::DownloadTypeToString(
53-
DownloadType::Engine)) /
54-
engine;
55-
56-
if (!std::filesystem::exists(engine_folder_path)) {
57-
CTL_INF("Creating " << engine_folder_path.string());
58-
std::filesystem::create_directories(engine_folder_path);
59-
}
60-
auto local_path = engine_folder_path / assetName;
61-
auto downloadTask{DownloadTask{.id = engine,
62-
.type = DownloadType::Engine,
63-
.items = {DownloadItem{
64-
.id = engine,
65-
.downloadUrl = download_url,
66-
.localPath = local_path,
67-
}}}};
68-
69-
DownloadService().AddAsyncDownloadTask(
70-
downloadTask, [](const DownloadTask& finishedTask) {
71-
// try to unzip the downloaded file
72-
archive_utils::ExtractArchive(
73-
finishedTask.items[0].localPath.string(),
74-
finishedTask.items[0]
75-
.localPath.parent_path()
76-
.parent_path()
77-
.string());
78-
79-
// remove the downloaded file
80-
try {
81-
std::filesystem::remove(finishedTask.items[0].localPath);
82-
} catch (const std::exception& e) {
83-
LOG_WARN << "Could not delete file: " << e.what();
84-
}
85-
LOG_INFO << "Finished!";
86-
});
87-
88-
Json::Value res;
89-
res["message"] = "Engine download started";
90-
res["result"] = "OK";
91-
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
92-
resp->setStatusCode(k200OK);
93-
callback(resp);
94-
return;
95-
}
96-
}
97-
Json::Value res;
98-
res["message"] = "Engine not found";
99-
res["result"] = "Error";
100-
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
101-
resp->setStatusCode(k404NotFound);
102-
callback(resp);
103-
} catch (const json::parse_error& e) {
104-
std::cerr << "JSON parse error: " << e.what() << std::endl;
105-
}
106-
}
24+
auto result = engine_service_.InstallEngine(engine, version);
25+
if (result.has_error()) {
26+
Json::Value res;
27+
res["message"] = result.error();
28+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
29+
resp->setStatusCode(k400BadRequest);
30+
callback(resp);
10731
} else {
108-
auto err = res.error();
109-
LOG_ERROR << "HTTP error: " << httplib::to_string(err);
32+
Json::Value res;
33+
res["message"] = "Engine " + engine + " installed successfully!";
34+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
35+
resp->setStatusCode(k200OK);
36+
callback(resp);
11037
}
11138
}
11239

11340
void Engines::ListEngine(
11441
const HttpRequestPtr& req,
11542
std::function<void(const HttpResponsePtr&)>&& callback) const {
116-
auto engine_service = EngineService();
117-
auto status_list = engine_service.GetEngineInfoList();
43+
auto status_list = engine_service_.GetEngineInfoList();
11844

11945
Json::Value ret;
12046
ret["object"] = "list";
@@ -140,10 +66,9 @@ void Engines::ListEngine(
14066
void Engines::GetEngine(const HttpRequestPtr& req,
14167
std::function<void(const HttpResponsePtr&)>&& callback,
14268
const std::string& engine) const {
143-
auto engine_service = EngineService();
144-
try {
145-
auto status = engine_service.GetEngineInfo(engine);
146-
Json::Value ret;
69+
auto status = engine_service_.GetEngineInfo(engine);
70+
Json::Value ret;
71+
if (status.has_value()) {
14772
ret["name"] = status->name;
14873
ret["description"] = status->description;
14974
ret["version"] = status->version;
@@ -153,48 +78,34 @@ void Engines::GetEngine(const HttpRequestPtr& req,
15378
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
15479
resp->setStatusCode(k200OK);
15580
callback(resp);
156-
} catch (const std::runtime_error e) {
81+
} else {
15782
Json::Value ret;
158-
ret["message"] = e.what();
83+
ret["message"] = "Engine not found";
15984
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
16085
resp->setStatusCode(k400BadRequest);
16186
callback(resp);
162-
} catch (const std::exception& e) {
163-
Json::Value ret;
164-
ret["message"] = e.what();
165-
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
166-
resp->setStatusCode(k500InternalServerError);
167-
callback(resp);
16887
}
16988
}
17089

17190
void Engines::UninstallEngine(
17291
const HttpRequestPtr& req,
17392
std::function<void(const HttpResponsePtr&)>&& callback,
174-
const std::string& engine) const {
175-
LOG_INFO << "[Http] Uninstall engine " << engine;
176-
auto engine_service = EngineService();
93+
const std::string& engine) {
17794

95+
auto result = engine_service_.UninstallEngine(engine);
17896
Json::Value ret;
179-
try {
180-
// TODO: Unload the model which is currently running on engine_
181-
// TODO: Unload engine if is loaded
182-
engine_service.UninstallEngine(engine);
18397

184-
ret["message"] = "Engine " + engine + " uninstalled successfully!";
185-
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
186-
resp->setStatusCode(k200OK);
187-
callback(resp);
188-
} catch (const std::runtime_error& e) {
189-
CLI_LOG("Runtime exception");
190-
ret["message"] = "Engine " + engine + " is not installed!";
98+
if (result.has_error()) {
99+
CTL_INF(result.error());
100+
ret["message"] = result.error();
191101
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
192102
resp->setStatusCode(k400BadRequest);
193103
callback(resp);
194-
} catch (const std::exception& e) {
195-
ret["message"] = "Engine " + engine + " failed to uninstall: " + e.what();
104+
} else {
105+
CTL_INF("Engine uninstalled successfully");
106+
ret["message"] = "Engine " + engine + " uninstalled successfully!";
196107
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
197-
resp->setStatusCode(k400BadRequest);
108+
resp->setStatusCode(k200OK);
198109
callback(resp);
199110
}
200111
}

engine/controllers/engines.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@
33
#include <drogon/HttpController.h>
44
#include <drogon/HttpRequest.h>
55
#include <trantor/utils/Logger.h>
6+
#include "services/engine_service.h"
67
#include "utils/cortexso_parser.h"
78

89
using namespace drogon;
910

1011
class Engines : public drogon::HttpController<Engines> {
1112
public:
13+
Engines() : engine_service_{EngineService()} {};
14+
1215
METHOD_LIST_BEGIN
13-
METHOD_ADD(Engines::InstallEngine, "/{1}/init", Post);
16+
METHOD_ADD(Engines::InstallEngine, "/{1}/install", Post);
1417
METHOD_ADD(Engines::UninstallEngine, "/{1}", Delete);
1518
METHOD_ADD(Engines::ListEngine, "", Get);
1619
METHOD_ADD(Engines::GetEngine, "/{1}", Get);
1720
METHOD_LIST_END
1821

1922
void InstallEngine(const HttpRequestPtr& req,
2023
std::function<void(const HttpResponsePtr&)>&& callback,
21-
const std::string& engine) const;
24+
const std::string& engine);
2225

2326
void ListEngine(const HttpRequestPtr& req,
2427
std::function<void(const HttpResponsePtr&)>&& callback) const;
@@ -29,5 +32,8 @@ class Engines : public drogon::HttpController<Engines> {
2932

3033
void UninstallEngine(const HttpRequestPtr& req,
3134
std::function<void(const HttpResponsePtr&)>&& callback,
32-
const std::string& engine) const;
35+
const std::string& engine);
36+
37+
private:
38+
EngineService engine_service_;
3339
};

0 commit comments

Comments
 (0)