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

Commit 3dba0ce

Browse files
committed
feat: engine management
1 parent 10b64ee commit 3dba0ce

37 files changed

+1691
-858
lines changed

engine/cli/command_line_parser.cc

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include "command_line_parser.h"
22
#include <memory>
33
#include <optional>
4+
#include <string>
45
#include "commands/cortex_upd_cmd.h"
56
#include "commands/engine_get_cmd.h"
67
#include "commands/engine_install_cmd.h"
78
#include "commands/engine_list_cmd.h"
9+
#include "commands/engine_release_cmd.h"
810
#include "commands/engine_uninstall_cmd.h"
9-
#include "commands/model_alias_cmd.h"
1011
#include "commands/model_del_cmd.h"
1112
#include "commands/model_get_cmd.h"
1213
#include "commands/model_import_cmd.h"
@@ -268,30 +269,6 @@ void CommandLineParser::SetupModelCommands() {
268269
cml_data_.model_id);
269270
});
270271

271-
std::string model_alias;
272-
auto model_alias_cmd =
273-
models_cmd->add_subcommand("alias", "Add alias name for a modelID");
274-
model_alias_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
275-
" models alias --model_id [model_id] --alias [alias]");
276-
model_alias_cmd->group(kSubcommands);
277-
model_alias_cmd->add_option(
278-
"--model_id", cml_data_.model_id,
279-
"Can be modelID or model alias to identify model");
280-
model_alias_cmd->add_option("--alias", cml_data_.model_alias,
281-
"new alias to be set");
282-
model_alias_cmd->callback([this, model_alias_cmd]() {
283-
if (std::exchange(executed_, true))
284-
return;
285-
if (cml_data_.model_id.empty() || cml_data_.model_alias.empty()) {
286-
CLI_LOG("[model_id] and [alias] are required\n");
287-
CLI_LOG(model_alias_cmd->help());
288-
return;
289-
}
290-
commands::ModelAliasCmd mdc;
291-
mdc.Exec(cml_data_.config.apiServerHost,
292-
std::stoi(cml_data_.config.apiServerPort), cml_data_.model_id,
293-
cml_data_.model_alias);
294-
});
295272
// Model update parameters comment
296273
ModelUpdate(models_cmd);
297274

@@ -346,6 +323,21 @@ void CommandLineParser::SetupEngineCommands() {
346323
std::stoi(cml_data_.config.apiServerPort));
347324
});
348325

326+
auto installv2_cmd = engines_cmd->add_subcommand("release", "Install engine");
327+
installv2_cmd->group(kSubcommands);
328+
installv2_cmd->callback([this, installv2_cmd] {
329+
if (std::exchange(executed_, true))
330+
return;
331+
if (installv2_cmd->get_subcommands().empty()) {
332+
CLI_LOG("[engine_name] is required\n");
333+
CLI_LOG(installv2_cmd->help());
334+
}
335+
});
336+
for (auto& engine : engine_service_.kSupportEngines) {
337+
std::string engine_name{engine};
338+
EngineInstallV2(installv2_cmd, engine_name);
339+
}
340+
349341
auto install_cmd = engines_cmd->add_subcommand("install", "Install engine");
350342
install_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
351343
" engines install [engine_name] [options]");
@@ -444,6 +436,25 @@ void CommandLineParser::SetupSystemCommands() {
444436
});
445437
}
446438

439+
void CommandLineParser::EngineInstallV2(CLI::App* parent,
440+
const std::string& engine_name) {
441+
auto install_engine_cmd = parent->add_subcommand(engine_name, "");
442+
install_engine_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
443+
" engines install " + engine_name + " [options]");
444+
install_engine_cmd->group(kEngineGroup);
445+
install_engine_cmd->callback([this, engine_name] {
446+
if (std::exchange(executed_, true))
447+
return;
448+
auto result = commands::EngineReleaseCmd().Exec(
449+
cml_data_.config.apiServerHost,
450+
std::stoi(cml_data_.config.apiServerPort), engine_name);
451+
452+
if (result.has_error()) {
453+
CLI_LOG(result.error());
454+
}
455+
});
456+
}
457+
447458
void CommandLineParser::EngineInstall(CLI::App* parent,
448459
const std::string& engine_name,
449460
std::string& version, std::string& src) {

engine/cli/command_line_parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class CommandLineParser {
2222

2323
void SetupSystemCommands();
2424

25+
void EngineInstallV2(CLI::App* parent, const std::string& engine_name);
26+
2527
void EngineInstall(CLI::App* parent, const std::string& engine_name,
2628
std::string& version, std::string& src);
2729

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "engine_release_cmd.h"
2+
#include <vector>
3+
#include "commands/server_start_cmd.h"
4+
#include "utils/cli_selection_utils.h"
5+
#include "utils/curl_utils.h"
6+
#include "utils/github_release_utils.h"
7+
#include "utils/logging_utils.h"
8+
#include "utils/url_parser.h"
9+
10+
namespace commands {
11+
12+
cpp::result<void, std::string> EngineReleaseCmd::Exec(
13+
const std::string& host, int port, const std::string& engine_name) {
14+
15+
// Start server if server is not started yet
16+
if (!commands::IsServerAlive(host, port)) {
17+
CLI_LOG("Starting server ...");
18+
commands::ServerStartCmd ssc;
19+
if (!ssc.Exec(host, port)) {
20+
return cpp::fail("Failed to start server");
21+
}
22+
}
23+
24+
auto url_obj = url_parser::Url{
25+
.protocol = "http",
26+
.host = host + ":" + std::to_string(port),
27+
.pathParams = {"v1", "engines", engine_name, "releases"},
28+
};
29+
30+
auto res = curl_utils::SimpleGetJson(url_obj.ToFullPath());
31+
if (res.has_error()) {
32+
CLI_LOG("url: " + url_obj.ToFullPath());
33+
return cpp::fail("Failed to get engine release: " + engine_name +
34+
", error: " + res.error());
35+
}
36+
if (res.value().size() == 0) {
37+
return cpp::fail("No release found for engine: " + engine_name);
38+
}
39+
40+
std::vector<std::string> selections;
41+
for (const auto& release : res.value()) {
42+
selections.push_back(release["tag_name"].asString());
43+
}
44+
45+
auto selection =
46+
cli_selection_utils::PrintSelection(selections, "Available versions:");
47+
if (!selection.has_value()) {
48+
return cpp::fail("Invalid selection!"); // TODO: return latest version
49+
}
50+
51+
CLI_LOG("Selected " + engine_name + " version " + selection.value());
52+
Json::Value selected_release;
53+
for (const auto& release : res.value()) {
54+
if (release["tag_name"].asString() == selection.value()) {
55+
selected_release = release;
56+
break;
57+
}
58+
}
59+
60+
std::vector<std::string> variant_selections;
61+
for (const auto& variant : selected_release["assets"]) {
62+
variant_selections.push_back(variant["name"].asString());
63+
}
64+
65+
auto variant_selection = cli_selection_utils::PrintSelection(
66+
variant_selections, "Available variant:");
67+
if (!variant_selection.has_value()) {
68+
return cpp::fail("Invalid variant selection!");
69+
}
70+
71+
CLI_LOG("Selected " + variant_selection.value());
72+
github_release_utils::GitHubAsset selected_asset;
73+
for (const auto& asset : selected_release["assets"]) {
74+
if (asset["name"] == variant_selection) {
75+
selected_asset = github_release_utils::GitHubAsset::FromJson(asset);
76+
break;
77+
}
78+
}
79+
80+
// proceed to download
81+
82+
return {};
83+
}
84+
}; // namespace commands
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include "utils/result.hpp"
5+
6+
namespace commands {
7+
class EngineReleaseCmd {
8+
public:
9+
cpp::result<void, std::string> Exec(const std::string& host, int port,
10+
const std::string& engine_name);
11+
};
12+
13+
} // namespace commands

engine/cli/commands/model_alias_cmd.cc

Lines changed: 0 additions & 42 deletions
This file was deleted.

engine/cli/commands/model_alias_cmd.h

Lines changed: 0 additions & 11 deletions
This file was deleted.

engine/cli/commands/model_start_cmd.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "model_start_cmd.h"
2-
#include "config/yaml_config.h"
32
#include "cortex_upd_cmd.h"
4-
#include "database/models.h"
53
#include "httplib.h"
64
#include "run_cmd.h"
75
#include "server_start_cmd.h"

engine/cli/commands/run_cmd.cc

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,22 @@ void RunCmd::Exec(bool run_detach) {
104104

105105
// Check if engine existed. If not, download it
106106
{
107-
auto required_engine =
108-
engine_service_.GetEngineInfo(Repo2Engine(mc.engine));
109-
110-
if (!required_engine.has_value()) {
111-
throw std::runtime_error("Engine not found: " + mc.engine);
112-
}
113-
if (required_engine.value().status == EngineService::kIncompatible) {
114-
throw std::runtime_error("Engine " + mc.engine + " is incompatible");
115-
}
116-
if (required_engine.value().status == EngineService::kNotInstalled) {
117-
auto install_engine_result = engine_service_.InstallEngine(mc.engine);
118-
if (install_engine_result.has_error()) {
119-
throw std::runtime_error(install_engine_result.error());
120-
}
121-
}
107+
// TODO: NamH need refactor this
108+
// auto required_engine =
109+
// engine_service_.GetEngineInfo(Repo2Engine(mc.engine));
110+
//
111+
// if (!required_engine.has_value()) {
112+
// throw std::runtime_error("Engine not found: " + mc.engine);
113+
// }
114+
// if (required_engine.value().status == EngineService::kIncompatible) {
115+
// throw std::runtime_error("Engine " + mc.engine + " is incompatible");
116+
// }
117+
// if (required_engine.value().status == EngineService::kNotInstalled) {
118+
// auto install_engine_result = engine_service_.InstallEngine(mc.engine);
119+
// if (install_engine_result.has_error()) {
120+
// throw std::runtime_error(install_engine_result.error());
121+
// }
122+
// }
122123
}
123124

124125
// Start server if it is not running

engine/common/download_task.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <nlohmann/json.hpp>
66
#include <sstream>
77
#include <string>
8+
#include <unordered_map>
89

910
enum class DownloadType { Model, Engine, Miscellaneous, CudaToolkit, Cortex };
1011

@@ -14,6 +15,8 @@ struct DownloadItem {
1415

1516
std::string id;
1617

18+
std::optional<std::unordered_map<std::string, std::string>> headers;
19+
1720
std::string downloadUrl;
1821

1922
/**

engine/config/gguf_parser.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
#include <cstdint>
33
#include <cstring>
44
#include <ctime>
5-
#include <fstream>
65
#include <iostream>
7-
#include <map>
86
#include <regex>
97
#include <stdexcept>
108
#include <string>
@@ -41,7 +39,6 @@ void GGUFHandler::OpenFile(const std::string& file_path) {
4139
if (file_handle_ == INVALID_HANDLE_VALUE) {
4240
throw std::runtime_error("Failed to open file");
4341
}
44-
4542
// Get the file size
4643
LARGE_INTEGER file_size_struct;
4744
if (!GetFileSizeEx(file_handle_, &file_size_struct)) {

0 commit comments

Comments
 (0)