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

Commit f976757

Browse files
committed
update
1 parent 80da991 commit f976757

File tree

3 files changed

+114
-129
lines changed

3 files changed

+114
-129
lines changed

engine/cli/commands/chat_completion_cmd.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@
33
#include <string>
44
#include <vector>
55
#include "config/model_config.h"
6-
#include "services/model_service.h"
76

87
namespace commands {
98
class ChatCompletionCmd {
109
public:
11-
explicit ChatCompletionCmd(const ModelService& model_service)
12-
: model_service_{model_service} {};
13-
1410
void Exec(const std::string& host, int port, const std::string& model_handle,
1511
std::string msg);
1612
void Exec(const std::string& host, int port, const std::string& model_handle,
1713
const config::ModelConfig& mc, std::string msg);
1814

1915
private:
2016
std::vector<Json::Value> histories_;
21-
ModelService model_service_;
2217
};
2318
} // namespace commands

engine/cli/commands/cortex_upd_cmd.cc

Lines changed: 112 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "cortex_upd_cmd.h"
2-
#include "httplib.h"
2+
#include "cli/commands/server_start_cmd.h"
33
#include "server_stop_cmd.h"
44
#include "utils/archive_utils.h"
5+
#include "utils/curl_utils.h"
56
#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"
@@ -56,10 +56,12 @@ std::string GetNightlyInstallerName(const std::string& v,
5656
// C:\Users\vansa\AppData\Local\Temp\cortex\cortex-windows-amd64-network-installer.exe
5757
std::string GetInstallCmd(const std::string& exe_path) {
5858
#if defined(__APPLE__) && defined(__MACH__)
59-
return "sudo touch /var/tmp/cortex_installer_skip_postinstall_check && sudo installer "
59+
return "sudo touch /var/tmp/cortex_installer_skip_postinstall_check && sudo "
60+
"installer "
6061
"-pkg " +
6162
exe_path +
62-
" -target / && sudo rm /var/tmp/cortex_installer_skip_postinstall_check";
63+
" -target / && sudo rm "
64+
"/var/tmp/cortex_installer_skip_postinstall_check";
6365
#elif defined(__linux__)
6466
return "echo -e \"n\\n\" | sudo SKIP_POSTINSTALL=true apt install -y "
6567
"--allow-downgrades " +
@@ -137,64 +139,56 @@ std::optional<std::string> CheckNewUpdate(
137139
auto release_path = GetReleasePath();
138140
CTL_INF("Engine release path: " << host_name << release_path);
139141

140-
httplib::Client cli(host_name);
141-
if (timeout.has_value()) {
142-
cli.set_connection_timeout(*timeout);
143-
cli.set_read_timeout(*timeout);
142+
auto url_obj = url_parser::Url{
143+
.protocol = "https", .host = host_name, .pathParams = {}, // todo
144+
};
145+
auto res = curl_utils::SimpleGetJson(url_obj.ToFullPath());
146+
if (res.has_error()) {
147+
CTL_INF("HTTP error: " << res.error());
148+
return std::nullopt;
144149
}
145-
if (auto res = cli.Get(release_path)) {
146-
if (res->status == httplib::StatusCode::OK_200) {
147-
try {
148-
auto get_latest = [](const Json::Value& data) -> std::string {
149-
if (data.empty()) {
150-
return "";
151-
}
152150

153-
if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) {
154-
for (const auto& d : data) {
155-
if (auto tag = d["tag_name"].asString();
156-
tag.find(kBetaComp) != std::string::npos) {
157-
return tag;
158-
}
159-
}
160-
return data[0]["tag_name"].asString();
161-
} else {
162-
return data["tag_name"].asString();
151+
try {
152+
auto get_latest = [](const Json::Value& data) -> std::string {
153+
if (data.empty()) {
154+
return "";
155+
}
156+
157+
if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) {
158+
for (const auto& d : data) {
159+
if (auto tag = d["tag_name"].asString();
160+
tag.find(kBetaComp) != std::string::npos) {
161+
return tag;
163162
}
164-
return "";
165-
};
166-
167-
auto json_res = json_helper::ParseJsonString(res->body);
168-
std::string latest_version = get_latest(json_res);
169-
if (latest_version.empty()) {
170-
CTL_WRN("Release not found!");
171-
return std::nullopt;
172-
}
173-
std::string current_version = CORTEX_CPP_VERSION;
174-
CTL_INF("Got the latest release, update to the config file: "
175-
<< latest_version)
176-
config.latestRelease = latest_version;
177-
auto result = config_yaml_utils::DumpYamlConfig(
178-
config, file_manager_utils::GetConfigurationPath().string());
179-
if (result.has_error()) {
180-
CTL_ERR("Error update "
181-
<< file_manager_utils::GetConfigurationPath().string()
182-
<< result.error());
183163
}
184-
if (current_version != latest_version) {
185-
return latest_version;
186-
}
187-
} catch (const std::exception& e) {
188-
CTL_INF("JSON parse error: " << e.what());
189-
return std::nullopt;
164+
return data[0]["tag_name"].asString();
165+
} else {
166+
return data["tag_name"].asString();
190167
}
191-
} else {
192-
CTL_INF("HTTP error: " << res->status);
168+
return "";
169+
};
170+
171+
std::string latest_version = get_latest(res.value());
172+
if (latest_version.empty()) {
173+
CTL_WRN("Release not found!");
193174
return std::nullopt;
194175
}
195-
} else {
196-
auto err = res.error();
197-
CTL_INF("HTTP error: " << httplib::to_string(err));
176+
std::string current_version = CORTEX_CPP_VERSION;
177+
CTL_INF(
178+
"Got the latest release, update to the config file: " << latest_version)
179+
config.latestRelease = latest_version;
180+
auto result = config_yaml_utils::DumpYamlConfig(
181+
config, file_manager_utils::GetConfigurationPath().string());
182+
if (result.has_error()) {
183+
CTL_ERR("Error update "
184+
<< file_manager_utils::GetConfigurationPath().string()
185+
<< result.error());
186+
}
187+
if (current_version != latest_version) {
188+
return latest_version;
189+
}
190+
} catch (const std::exception& e) {
191+
CTL_INF("JSON parse error: " << e.what());
198192
return std::nullopt;
199193
}
200194
return std::nullopt;
@@ -281,9 +275,9 @@ void CortexUpdCmd::Exec(const std::string& v, bool force) {
281275

282276
{
283277
auto config = file_manager_utils::GetCortexConfig();
284-
httplib::Client cli(config.apiServerHost + ":" + config.apiServerPort);
285-
auto res = cli.Get("/healthz");
286-
if (res) {
278+
auto server_running = commands::IsServerAlive(
279+
config.apiServerHost, std::stoi(config.apiServerPort));
280+
if (server_running) {
287281
CLI_LOG("Server is running. Stopping server before updating!");
288282
commands::ServerStopCmd ssc(config.apiServerHost,
289283
std::stoi(config.apiServerPort));
@@ -323,33 +317,30 @@ bool CortexUpdCmd::GetStable(const std::string& v) {
323317
auto release_path = GetReleasePath();
324318
CTL_INF("Engine release path: " << github_host << release_path);
325319

326-
httplib::Client cli(github_host);
327-
if (auto res = cli.Get(release_path)) {
328-
if (res->status == httplib::StatusCode::OK_200) {
329-
try {
330-
auto json_data = json_helper::ParseJsonString(res->body);
331-
if (json_data.empty()) {
332-
CLI_LOG("Version not found: " << v);
333-
return false;
334-
}
320+
auto url_obj = url_parser::Url{
321+
.protocol = "https",
322+
.host = github_host,
323+
.pathParams = {},
324+
};
325+
auto res = curl_utils::SimpleGetJson(url_obj.ToFullPath());
326+
if (res.has_error()) {
327+
CLI_LOG_ERROR("HTTP error: " << res.error());
328+
return false;
329+
}
335330

336-
if (downloaded_exe_path = HandleGithubRelease(
337-
json_data["assets"],
338-
{system_info->os + "-" + system_info->arch});
339-
!downloaded_exe_path) {
340-
return false;
341-
}
342-
} catch (const std::exception& e) {
343-
CLI_LOG_ERROR("JSON parse error: " << e.what());
344-
return false;
345-
}
346-
} else {
347-
CLI_LOG_ERROR("HTTP error: " << res->status);
331+
try {
332+
if (res.value().empty()) {
333+
CLI_LOG("Version not found: " << v);
348334
return false;
349335
}
350-
} else {
351-
auto err = res.error();
352-
CLI_LOG_ERROR("HTTP error: " << httplib::to_string(err));
336+
337+
if (downloaded_exe_path = HandleGithubRelease(
338+
res.value()["assets"], {system_info->os + "-" + system_info->arch});
339+
!downloaded_exe_path) {
340+
return false;
341+
}
342+
} catch (const std::exception& e) {
343+
CLI_LOG_ERROR("JSON parse error: " << e.what());
353344
return false;
354345
}
355346

@@ -379,45 +370,41 @@ bool CortexUpdCmd::GetBeta(const std::string& v) {
379370
auto release_path = GetReleasePath();
380371
CTL_INF("Engine release path: " << github_host << release_path);
381372

382-
httplib::Client cli(github_host);
383-
if (auto res = cli.Get(release_path)) {
384-
if (res->status == httplib::StatusCode::OK_200) {
385-
try {
386-
auto json_res = json_helper::ParseJsonString(res->body);
387-
388-
Json::Value json_data;
389-
for (const auto& jr : json_res) {
390-
// Get the latest beta or match version
391-
if (auto tag = jr["tag_name"].asString();
392-
(v.empty() && tag.find(kBetaComp) != std::string::npos) ||
393-
(tag == v)) {
394-
json_data = jr;
395-
break;
396-
}
397-
}
373+
auto url_obj = url_parser::Url{
374+
.protocol = "https", .host = github_host, .pathParams = {}, // TODO: namh
375+
};
398376

399-
if (json_data.empty()) {
400-
CLI_LOG("Version not found: " << v);
401-
return false;
402-
}
377+
// todo:: host is contains protocol
378+
auto res = curl_utils::SimpleGetJson(url_obj.ToFullPath());
379+
if (res.has_error()) {
380+
CLI_LOG_ERROR("HTTP error: " << res.error());
381+
return false;
382+
}
403383

404-
if (downloaded_exe_path = HandleGithubRelease(
405-
json_data["assets"],
406-
{system_info->os + "-" + system_info->arch});
407-
!downloaded_exe_path) {
408-
return false;
409-
}
410-
} catch (const std::exception& e) {
411-
CLI_LOG_ERROR("JSON parse error: " << e.what());
412-
return false;
384+
try {
385+
Json::Value json_data;
386+
for (const auto& jr : res.value()) {
387+
// Get the latest beta or match version
388+
if (auto tag = jr["tag_name"].asString();
389+
(v.empty() && tag.find(kBetaComp) != std::string::npos) ||
390+
(tag == v)) {
391+
json_data = jr;
392+
break;
413393
}
414-
} else {
415-
CLI_LOG_ERROR("HTTP error: " << res->status);
394+
}
395+
396+
if (json_data.empty()) {
397+
CLI_LOG("Version not found: " << v);
416398
return false;
417399
}
418-
} else {
419-
auto err = res.error();
420-
CLI_LOG_ERROR("HTTP error: " << httplib::to_string(err));
400+
401+
if (downloaded_exe_path = HandleGithubRelease(
402+
json_data["assets"], {system_info->os + "-" + system_info->arch});
403+
!downloaded_exe_path) {
404+
return false;
405+
}
406+
} catch (const std::exception& e) {
407+
CLI_LOG_ERROR("JSON parse error: " << e.what());
421408
return false;
422409
}
423410

@@ -473,13 +460,15 @@ std::optional<std::string> CortexUpdCmd::HandleGithubRelease(
473460
CLI_LOG_ERROR("Failed to create directories: " << e.what());
474461
return std::nullopt;
475462
}
476-
auto download_task{DownloadTask{.id = "cortex",
477-
.type = DownloadType::Cortex,
478-
.items = {DownloadItem{
479-
.id = "cortex",
480-
.downloadUrl = download_url,
481-
.localPath = local_path,
482-
}}}};
463+
auto download_task{DownloadTask{
464+
.id = "cortex",
465+
.type = DownloadType::Cortex,
466+
.items = {DownloadItem{
467+
.id = "cortex",
468+
.downloadUrl = download_url,
469+
.localPath = local_path,
470+
}},
471+
}};
483472

484473
auto result = download_service_->AddDownloadTask(
485474
download_task, [](const DownloadTask& finishedTask) {

engine/services/model_service.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ cpp::result<DownloadTask, std::string> GetDownloadTask(
7878
url_parser::Url url = {
7979
.protocol = "https",
8080
.host = kHuggingFaceHost,
81-
.pathParams = {"api", "models", "cortexso", modelId, "tree", branch}};
81+
.pathParams = {"api", "models", "cortexso", modelId, "tree", branch},
82+
};
8283

8384
auto result = curl_utils::SimpleGetJson(url.ToFullPath());
8485
if (result.has_error()) {

0 commit comments

Comments
 (0)