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

Commit 6f22aa1

Browse files
authored
fix: Cortex CLI should be a layer on top of the API Server (#1489)
* fix: update swagger * fix: start stop model with API call * fix: model del * fix: model get * fix: model alias * fix: model import * fix: model status * fix: model list * fix: models update * fix: engine list * fix: engine get * fix: engine uninstall
1 parent 170c508 commit 6f22aa1

26 files changed

+602
-695
lines changed

engine/commands/engine_get_cmd.cc

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,53 @@
11
#include "engine_get_cmd.h"
22
#include <iostream>
3-
#include <tabulate/table.hpp>
4-
#include "services/engine_service.h"
3+
4+
#include "httplib.h"
5+
#include "json/json.h"
6+
#include "server_start_cmd.h"
57
#include "utils/logging_utils.h"
68

9+
// clang-format off
10+
#include <tabulate/table.hpp>
11+
// clang-format on
12+
713
namespace commands {
814

9-
void EngineGetCmd::Exec(const std::string& engine_name) const {
10-
auto engine = engine_service_.GetEngineInfo(engine_name);
11-
if (engine.has_error()) {
12-
CLI_LOG(engine.error());
13-
return;
15+
void EngineGetCmd::Exec(const std::string& host, int port,
16+
const std::string& engine_name) const {
17+
// Start server if server is not started yet
18+
if (!commands::IsServerAlive(host, port)) {
19+
CLI_LOG("Starting server ...");
20+
commands::ServerStartCmd ssc;
21+
if (!ssc.Exec(host, port)) {
22+
return;
23+
}
1424
}
1525

16-
auto version = engine->version.value_or("");
17-
auto variant = engine->variant.value_or("");
1826
tabulate::Table table;
1927
table.add_row({"Name", "Supported Formats", "Version", "Variant", "Status"});
20-
table.add_row(
21-
{engine->product_name, engine->format, version, variant, engine->status});
28+
httplib::Client cli(host + ":" + std::to_string(port));
29+
auto res = cli.Get("/v1/engines/" + engine_name);
30+
if (res) {
31+
if (res->status == httplib::StatusCode::OK_200) {
32+
// CLI_LOG(res->body);
33+
Json::Value v;
34+
Json::Reader reader;
35+
reader.parse(res->body, v);
36+
37+
table.add_row({v["name"].asString(), v["format"].asString(),
38+
v["version"].asString(), v["variant"].asString(),
39+
v["status"].asString()});
40+
41+
} else {
42+
CTL_ERR("Failed to get engine list with status code: " << res->status);
43+
return;
44+
}
45+
} else {
46+
auto err = res.error();
47+
CTL_ERR("HTTP error: " << httplib::to_string(err));
48+
return;
49+
}
50+
2251
std::cout << table << std::endl;
2352
}
2453
}; // namespace commands

engine/commands/engine_get_cmd.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
#pragma once
2-
3-
#include "services/engine_service.h"
2+
#include <string>
43

54
namespace commands {
65
class EngineGetCmd {
76
public:
8-
explicit EngineGetCmd(std::shared_ptr<DownloadService> download_service)
9-
: engine_service_{EngineService(download_service)} {};
10-
11-
void Exec(const std::string& engineName) const;
7+
void Exec(const std::string& host, int port,
8+
const std::string& engineName) const;
129

13-
private:
14-
EngineService engine_service_;
1510
};
1611
} // namespace commands

engine/commands/engine_list_cmd.cc

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,53 @@
11
#include "engine_list_cmd.h"
2+
#include "httplib.h"
3+
#include "json/json.h"
4+
#include "server_start_cmd.h"
5+
#include "utils/logging_utils.h"
6+
// clang-format off
27
#include <tabulate/table.hpp>
8+
// clang-format on
39

410
namespace commands {
511

6-
bool EngineListCmd::Exec() {
7-
auto status_list = engine_service_.GetEngineInfoList();
12+
bool EngineListCmd::Exec(const std::string& host, int port) {
13+
// Start server if server is not started yet
14+
if (!commands::IsServerAlive(host, port)) {
15+
CLI_LOG("Starting server ...");
16+
commands::ServerStartCmd ssc;
17+
if (!ssc.Exec(host, port)) {
18+
return false;
19+
}
20+
}
821

922
tabulate::Table table;
1023
table.add_row(
1124
{"#", "Name", "Supported Formats", "Version", "Variant", "Status"});
12-
for (int i = 0; i < status_list.size(); i++) {
13-
auto engine_status = status_list[i];
14-
std::string index = std::to_string(i + 1);
15-
auto variant = engine_status.variant.value_or("");
16-
auto version = engine_status.version.value_or("");
17-
table.add_row({index, engine_status.product_name, engine_status.format,
18-
version, variant, engine_status.status});
25+
26+
httplib::Client cli(host + ":" + std::to_string(port));
27+
auto res = cli.Get("/v1/engines");
28+
if (res) {
29+
if (res->status == httplib::StatusCode::OK_200) {
30+
int count = 0;
31+
// CLI_LOG(res->body);
32+
Json::Value body;
33+
Json::Reader reader;
34+
reader.parse(res->body, body);
35+
if (!body["data"].isNull()) {
36+
for (auto const& v : body["data"]) {
37+
count += 1;
38+
table.add_row({std::to_string(count), v["name"].asString(),
39+
v["format"].asString(), v["version"].asString(),
40+
v["variant"].asString(), v["status"].asString()});
41+
}
42+
}
43+
} else {
44+
CTL_ERR("Failed to get engine list with status code: " << res->status);
45+
return false;
46+
}
47+
} else {
48+
auto err = res.error();
49+
CTL_ERR("HTTP error: " << httplib::to_string(err));
50+
return false;
1951
}
2052

2153
std::cout << table << std::endl;

engine/commands/engine_list_cmd.h

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

3-
#include "services/engine_service.h"
3+
#include <string>
44

55
namespace commands {
66
class EngineListCmd {
77
public:
8-
explicit EngineListCmd(std::shared_ptr<DownloadService> download_service)
9-
: engine_service_{EngineService(download_service)} {};
10-
11-
bool Exec();
12-
13-
private:
14-
EngineService engine_service_;
8+
bool Exec(const std::string& host, int port);
159
};
1610

1711
} // namespace commands
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
#include "engine_uninstall_cmd.h"
2-
#include "services/engine_service.h"
2+
#include "httplib.h"
3+
#include "server_start_cmd.h"
34
#include "utils/logging_utils.h"
45

56
namespace commands {
67

7-
void EngineUninstallCmd::Exec(const std::string& engine) {
8-
auto result = engine_service_.UninstallEngine(engine);
8+
void EngineUninstallCmd::Exec(const std::string& host, int port,
9+
const std::string& engine) {
10+
// Start server if server is not started yet
11+
if (!commands::IsServerAlive(host, port)) {
12+
CLI_LOG("Starting server ...");
13+
commands::ServerStartCmd ssc;
14+
if (!ssc.Exec(host, port)) {
15+
return;
16+
}
17+
}
918

10-
if (result.has_error()) {
11-
CLI_LOG(result.error());
19+
// Call API to delete engine
20+
httplib::Client cli(host + ":" + std::to_string(port));
21+
auto res = cli.Delete("/v1/engines/" + engine);
22+
if (res) {
23+
if (res->status == httplib::StatusCode::OK_200) {
24+
CLI_LOG("Engine " + engine + " uninstalled successfully");
25+
} else {
26+
CTL_ERR("Engine failed to uninstall with status code: " << res->status);
27+
}
1228
} else {
13-
CLI_LOG("Engine " + engine + " uninstalled successfully!");
29+
auto err = res.error();
30+
CTL_ERR("HTTP error: " << httplib::to_string(err));
1431
}
1532
}
1633
}; // namespace commands
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
#pragma once
22

3-
#include <memory>
43
#include <string>
5-
#include "services/engine_service.h"
64

75
namespace commands {
86
class EngineUninstallCmd {
97
public:
10-
explicit EngineUninstallCmd(std::shared_ptr<DownloadService> download_service)
11-
: engine_service_{EngineService(download_service)} {};
12-
13-
void Exec(const std::string& engine);
14-
15-
private:
16-
EngineService engine_service_;
8+
void Exec(const std::string& host, int port, const std::string& engine);
179
};
1810
} // namespace commands

engine/commands/model_alias_cmd.cc

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
#include "model_alias_cmd.h"
22
#include "database/models.h"
3+
#include "httplib.h"
4+
#include "server_start_cmd.h"
5+
#include "json/json.h"
36

47
namespace commands {
58

6-
void ModelAliasCmd::Exec(const std::string& model_handle,
9+
void ModelAliasCmd::Exec(const std::string& host, int port,
10+
const std::string& model_handle,
711
const std::string& model_alias) {
8-
cortex::db::Models modellist_handler;
9-
try {
10-
auto result = modellist_handler.UpdateModelAlias(model_handle, model_alias);
11-
if (result.has_error()) {
12-
CLI_LOG(result.error());
13-
} else {
14-
if (result.value()) {
15-
CLI_LOG("Successfully set model alias '" + model_alias +
16-
"' for modeID '" + model_handle + "'.");
17-
} else {
18-
CLI_LOG("Unable to set model alias for modelID '" + model_handle +
19-
"': model alias '" + model_alias + "' is not unique!");
20-
}
12+
// Start server if server is not started yet
13+
if (!commands::IsServerAlive(host, port)) {
14+
CLI_LOG("Starting server ...");
15+
commands::ServerStartCmd ssc;
16+
if (!ssc.Exec(host, port)) {
17+
return;
2118
}
19+
}
2220

23-
} catch (const std::exception& e) {
24-
CLI_LOG("Error when setting model alias ('" + model_alias +
25-
"') for modelID '" + model_handle + "':" + e.what());
21+
// Call API to delete model
22+
httplib::Client cli(host + ":" + std::to_string(port));
23+
Json::Value json_data;
24+
json_data["model"] = model_handle;
25+
json_data["modelAlias"] = model_alias;
26+
auto data_str = json_data.toStyledString();
27+
auto res = cli.Post("/v1/models/alias", httplib::Headers(), data_str.data(),
28+
data_str.size(), "application/json");
29+
if (res) {
30+
if (res->status == httplib::StatusCode::OK_200) {
31+
CLI_LOG("Successfully set model alias '" + model_alias +
32+
"' for modeID '" + model_handle + "'.");
33+
} else {
34+
CTL_ERR("Model failed to set alias with status code: " << res->status);
35+
}
36+
} else {
37+
auto err = res.error();
38+
CTL_ERR("HTTP error: " << httplib::to_string(err));
2639
}
2740
}
2841

engine/commands/model_alias_cmd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace commands {
55

66
class ModelAliasCmd {
77
public:
8-
void Exec(const std::string& model_handle, const std::string& model_alias);
8+
void Exec(const std::string& host, int port, const std::string& model_handle,
9+
const std::string& model_alias);
910
};
1011
} // namespace commands

engine/commands/model_del_cmd.cc

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
#include "model_del_cmd.h"
2+
#include "httplib.h"
3+
#include "server_start_cmd.h"
24
#include "utils/logging_utils.h"
35

46
namespace commands {
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());
7+
8+
void ModelDelCmd::Exec(const std::string& host, int port,
9+
const std::string& model_handle) {
10+
// Start server if server is not started yet
11+
if (!commands::IsServerAlive(host, port)) {
12+
CLI_LOG("Starting server ...");
13+
commands::ServerStartCmd ssc;
14+
if (!ssc.Exec(host, port)) {
15+
return;
16+
}
17+
}
18+
19+
// Call API to delete model
20+
httplib::Client cli(host + ":" + std::to_string(port));
21+
auto res = cli.Delete("/v1/models/" + model_handle);
22+
if (res) {
23+
if (res->status == httplib::StatusCode::OK_200) {
24+
CLI_LOG("Model " + model_handle + " deleted successfully");
25+
} else {
26+
CTL_ERR("Model failed to delete with status code: " << res->status);
27+
}
928
} else {
10-
CLI_LOG("Model " + model_handle + " deleted successfully");
29+
auto err = res.error();
30+
CTL_ERR("HTTP error: " << httplib::to_string(err));
1131
}
1232
}
1333
} // namespace commands

engine/commands/model_del_cmd.h

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

33
#include <string>
4-
#include "services/model_service.h"
54

65
namespace commands {
76

87
class ModelDelCmd {
98
public:
10-
explicit ModelDelCmd(std::shared_ptr<DownloadService> download_service)
11-
: model_service_{ModelService(download_service)} {};
12-
13-
void Exec(const std::string& model_handle);
14-
15-
private:
16-
ModelService model_service_;
9+
void Exec(const std::string& host, int port, const std::string& model_handle);
1710
};
1811
} // namespace commands

0 commit comments

Comments
 (0)