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

Commit 498d8ff

Browse files
committed
feat: engine management
1 parent ee41d48 commit 498d8ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2064
-1157
lines changed

engine/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ find_package(jsoncpp CONFIG REQUIRED)
7373
find_package(Drogon CONFIG REQUIRED)
7474
find_package(yaml-cpp CONFIG REQUIRED)
7575
find_package(httplib CONFIG REQUIRED)
76-
find_package(nlohmann_json CONFIG REQUIRED)
7776
find_package(unofficial-minizip CONFIG REQUIRED)
7877
find_package(LibArchive REQUIRED)
7978
find_package(CURL REQUIRED)
@@ -149,7 +148,6 @@ add_executable(${TARGET_NAME} main.cc
149148
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
150149

151150
target_link_libraries(${TARGET_NAME} PRIVATE httplib::httplib)
152-
target_link_libraries(${TARGET_NAME} PRIVATE nlohmann_json::nlohmann_json)
153151
target_link_libraries(${TARGET_NAME} PRIVATE unofficial::minizip::minizip)
154152
target_link_libraries(${TARGET_NAME} PRIVATE LibArchive::LibArchive)
155153
target_link_libraries(${TARGET_NAME} PRIVATE CURL::libcurl)

engine/cli/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ add_compile_definitions(CORTEX_CONFIG_FILE_PATH="${CORTEX_CONFIG_FILE_PATH}")
6363
find_package(jsoncpp CONFIG REQUIRED)
6464
find_package(yaml-cpp CONFIG REQUIRED)
6565
find_package(httplib CONFIG REQUIRED)
66-
find_package(nlohmann_json CONFIG REQUIRED)
6766
find_package(CLI11 CONFIG REQUIRED)
6867
find_package(unofficial-minizip CONFIG REQUIRED)
6968
find_package(LibArchive REQUIRED)
@@ -87,7 +86,6 @@ add_executable(${TARGET_NAME} main.cc
8786
)
8887

8988
target_link_libraries(${TARGET_NAME} PRIVATE httplib::httplib)
90-
target_link_libraries(${TARGET_NAME} PRIVATE nlohmann_json::nlohmann_json)
9189
target_link_libraries(${TARGET_NAME} PRIVATE CLI11::CLI11)
9290
target_link_libraries(${TARGET_NAME} PRIVATE unofficial::minizip::minizip)
9391
target_link_libraries(${TARGET_NAME} PRIVATE LibArchive::LibArchive)
@@ -128,4 +126,4 @@ set_target_properties(${TARGET_NAME} PROPERTIES
128126
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}
129127
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}
130128
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
131-
)
129+
)

engine/cli/command_line_parser.cc

Lines changed: 41 additions & 30 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"
@@ -94,8 +95,8 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
9495
CLI_LOG("\nNew Cortex release available: "
9596
<< CORTEX_CPP_VERSION << " -> " << *latest_version);
9697
CLI_LOG("To update, run: " << commands::GetRole()
97-
<< commands::GetCortexBinary()
98-
<< " update");
98+
<< commands::GetCortexBinary()
99+
<< " update");
99100
}
100101
done = true;
101102
});
@@ -138,7 +139,8 @@ void CommandLineParser::SetupCommonCommands() {
138139
}
139140
});
140141

141-
auto run_cmd = app_.add_subcommand("run", "Shortcut: pull, start & chat with a model");
142+
auto run_cmd =
143+
app_.add_subcommand("run", "Shortcut: pull, start & chat with a model");
142144
run_cmd->group(kCommonCommandsGroup);
143145
run_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
144146
" run [options] [model_id]");
@@ -270,30 +272,6 @@ void CommandLineParser::SetupModelCommands() {
270272
cml_data_.model_id);
271273
});
272274

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

@@ -348,6 +326,21 @@ void CommandLineParser::SetupEngineCommands() {
348326
std::stoi(cml_data_.config.apiServerPort));
349327
});
350328

329+
auto installv2_cmd = engines_cmd->add_subcommand("release", "Install engine");
330+
installv2_cmd->group(kSubcommands);
331+
installv2_cmd->callback([this, installv2_cmd] {
332+
if (std::exchange(executed_, true))
333+
return;
334+
if (installv2_cmd->get_subcommands().empty()) {
335+
CLI_LOG("[engine_name] is required\n");
336+
CLI_LOG(installv2_cmd->help());
337+
}
338+
});
339+
for (auto& engine : engine_service_.kSupportEngines) {
340+
std::string engine_name{engine};
341+
EngineInstallV2(installv2_cmd, engine_name);
342+
}
343+
351344
auto install_cmd = engines_cmd->add_subcommand("install", "Install engine");
352345
install_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
353346
" engines install [engine_name] [options]");
@@ -417,8 +410,7 @@ void CommandLineParser::SetupSystemCommands() {
417410
ssc.Exec();
418411
});
419412

420-
auto ps_cmd =
421-
app_.add_subcommand("ps", "Show active model statuses");
413+
auto ps_cmd = app_.add_subcommand("ps", "Show active model statuses");
422414
ps_cmd->group(kSystemGroup);
423415
ps_cmd->usage("Usage:\n" + commands::GetCortexBinary() + "ps");
424416
ps_cmd->callback([&]() {
@@ -446,6 +438,25 @@ void CommandLineParser::SetupSystemCommands() {
446438
});
447439
}
448440

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

engine/cli/commands/chat_completion_cmd.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ struct ChunkParser {
2929
is_done = true;
3030
} else {
3131
try {
32-
content = nlohmann::json::parse(s)["choices"][0]["delta"]["content"];
33-
} catch (const nlohmann::json::parse_error& e) {
32+
content =
33+
json_helper::ParseJsonString(s)["choices"][0]["delta"]["content"]
34+
.asString();
35+
} catch (const std::exception& e) {
3436
CTL_WRN("JSON parse error: " << e.what());
3537
}
3638
}

engine/cli/commands/cortex_upd_cmd.cc

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "cortex_upd_cmd.h"
22
#include "httplib.h"
3-
#include "nlohmann/json.hpp"
43
#include "server_stop_cmd.h"
54
#include "utils/archive_utils.h"
65
#include "utils/file_manager_utils.h"
6+
#include "utils/json_helper.h"
77
#include "utils/logging_utils.h"
88
#include "utils/scope_exit.h"
99
#include "utils/system_info_utils.h"
@@ -144,26 +144,26 @@ std::optional<std::string> CheckNewUpdate(
144144
if (auto res = cli.Get(release_path)) {
145145
if (res->status == httplib::StatusCode::OK_200) {
146146
try {
147-
auto get_latest = [](const nlohmann::json& data) -> std::string {
147+
auto get_latest = [](const Json::Value& data) -> std::string {
148148
if (data.empty()) {
149149
return "";
150150
}
151151

152152
if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) {
153-
for (auto& d : data) {
154-
if (auto tag = d["tag_name"].get<std::string>();
153+
for (const auto& d : data) {
154+
if (auto tag = d["tag_name"].asString();
155155
tag.find(kBetaComp) != std::string::npos) {
156156
return tag;
157157
}
158158
}
159-
return data[0]["tag_name"].get<std::string>();
159+
return data[0]["tag_name"].asString();
160160
} else {
161-
return data["tag_name"].get<std::string>();
161+
return data["tag_name"].asString();
162162
}
163163
return "";
164164
};
165165

166-
auto json_res = nlohmann::json::parse(res->body);
166+
auto json_res = json_helper::ParseJsonString(res->body);
167167
std::string latest_version = get_latest(json_res);
168168
if (latest_version.empty()) {
169169
CTL_WRN("Release not found!");
@@ -178,7 +178,7 @@ std::optional<std::string> CheckNewUpdate(
178178
if (current_version != latest_version) {
179179
return latest_version;
180180
}
181-
} catch (const nlohmann::json::parse_error& e) {
181+
} catch (const std::exception& e) {
182182
CTL_INF("JSON parse error: " << e.what());
183183
return std::nullopt;
184184
}
@@ -321,7 +321,7 @@ bool CortexUpdCmd::GetStable(const std::string& v) {
321321
if (auto res = cli.Get(release_path)) {
322322
if (res->status == httplib::StatusCode::OK_200) {
323323
try {
324-
auto json_data = nlohmann::json::parse(res->body);
324+
auto json_data = json_helper::ParseJsonString(res->body);
325325
if (json_data.empty()) {
326326
CLI_LOG("Version not found: " << v);
327327
return false;
@@ -333,7 +333,7 @@ bool CortexUpdCmd::GetStable(const std::string& v) {
333333
!downloaded_exe_path) {
334334
return false;
335335
}
336-
} catch (const nlohmann::json::parse_error& e) {
336+
} catch (const std::exception& e) {
337337
CLI_LOG_ERROR("JSON parse error: " << e.what());
338338
return false;
339339
}
@@ -377,12 +377,12 @@ bool CortexUpdCmd::GetBeta(const std::string& v) {
377377
if (auto res = cli.Get(release_path)) {
378378
if (res->status == httplib::StatusCode::OK_200) {
379379
try {
380-
auto json_res = nlohmann::json::parse(res->body);
380+
auto json_res = json_helper::ParseJsonString(res->body);
381381

382-
nlohmann::json json_data;
383-
for (auto& jr : json_res) {
382+
Json::Value json_data;
383+
for (const auto& jr : json_res) {
384384
// Get the latest beta or match version
385-
if (auto tag = jr["tag_name"].get<std::string>();
385+
if (auto tag = jr["tag_name"].asString();
386386
(v.empty() && tag.find(kBetaComp) != std::string::npos) ||
387387
(tag == v)) {
388388
json_data = jr;
@@ -401,7 +401,7 @@ bool CortexUpdCmd::GetBeta(const std::string& v) {
401401
!downloaded_exe_path) {
402402
return false;
403403
}
404-
} catch (const nlohmann::json::parse_error& e) {
404+
} catch (const std::exception& e) {
405405
CLI_LOG_ERROR("JSON parse error: " << e.what());
406406
return false;
407407
}
@@ -429,14 +429,13 @@ bool CortexUpdCmd::GetBeta(const std::string& v) {
429429

430430
assert(!!downloaded_exe_path);
431431
return InstallNewVersion(dst, downloaded_exe_path.value());
432-
;
433432
}
434433

435434
std::optional<std::string> CortexUpdCmd::HandleGithubRelease(
436-
const nlohmann::json& assets, const std::string& os_arch) {
435+
const Json::Value& assets, const std::string& os_arch) {
437436
std::string matched_variant = "";
438-
for (auto& asset : assets) {
439-
auto asset_name = asset["name"].get<std::string>();
437+
for (const auto& asset : assets) {
438+
auto asset_name = asset["name"].asString();
440439
if (asset_name.find(kCortexBinary) != std::string::npos &&
441440
asset_name.find(os_arch) != std::string::npos &&
442441
asset_name.find(kReleaseFormat) != std::string::npos) {
@@ -451,11 +450,11 @@ std::optional<std::string> CortexUpdCmd::HandleGithubRelease(
451450
}
452451
CTL_INF("Matched variant: " << matched_variant);
453452

454-
for (auto& asset : assets) {
455-
auto asset_name = asset["name"].get<std::string>();
453+
for (const auto& asset : assets) {
454+
auto asset_name = asset["name"].asString();
456455
if (asset_name == matched_variant) {
457-
auto download_url = asset["browser_download_url"].get<std::string>();
458-
auto file_name = asset["name"].get<std::string>();
456+
auto download_url = asset["browser_download_url"].asString();
457+
auto file_name = asset["name"].asString();
459458
CTL_INF("Download url: " << download_url);
460459

461460
auto local_path =

engine/cli/commands/cortex_upd_cmd.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <string>
3+
#include "services/download_service.h"
34
#if !defined(_WIN32)
45
#include <sys/stat.h>
56
#include <unistd.h>
@@ -57,7 +58,8 @@ inline std::string GetCortexServerBinary() {
5758
return has_exe ? kCortexServerBinary + "-nightly.exe"
5859
: kCortexServerBinary + "-nightly";
5960
} else if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) {
60-
return has_exe ? kCortexServerBinary + "-beta.exe" : kCortexServerBinary + "-beta";
61+
return has_exe ? kCortexServerBinary + "-beta.exe"
62+
: kCortexServerBinary + "-beta";
6163
} else {
6264
return has_exe ? kCortexServerBinary + ".exe" : kCortexServerBinary;
6365
}
@@ -104,8 +106,8 @@ class CortexUpdCmd {
104106

105107
bool GetStable(const std::string& v);
106108
bool GetBeta(const std::string& v);
107-
std::optional<std::string> HandleGithubRelease(const nlohmann::json& assets,
108-
const std::string& os_arch);
109+
std::optional<std::string> HandleGithubRelease(const Json::Value& assets,
110+
const std::string& os_arch);
109111
bool GetNightly(const std::string& v);
110112
};
111113
} // namespace commands

0 commit comments

Comments
 (0)