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

Commit a674b9c

Browse files
committed
update
1 parent ef9a966 commit a674b9c

File tree

6 files changed

+107
-33
lines changed

6 files changed

+107
-33
lines changed

engine/cli/command_line_parser.cc

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,28 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
8888

8989
// Check new update
9090
#ifdef CORTEX_CPP_VERSION
91-
if (cml_data_.check_upd) {
92-
if (strcmp(CORTEX_CPP_VERSION, "default_version") != 0) {
93-
// TODO(sang) find a better way to handle
94-
// This is an extremely ugly way to deal with connection
95-
// hang when network down
96-
std::atomic<bool> done = false;
97-
std::thread t([&]() {
98-
if (auto latest_version =
99-
commands::CheckNewUpdate(commands::kTimeoutCheckUpdate);
100-
latest_version.has_value() &&
101-
*latest_version != CORTEX_CPP_VERSION) {
102-
CLI_LOG("\nNew Cortex release available: "
103-
<< CORTEX_CPP_VERSION << " -> " << *latest_version);
104-
CLI_LOG("To update, run: " << commands::GetRole()
105-
<< commands::GetCortexBinary()
106-
<< " update");
107-
}
108-
done = true;
109-
});
110-
// Do not wait for http connection timeout
111-
t.detach();
112-
int retry = 10;
113-
while (!done && retry--) {
114-
std::this_thread::sleep_for(commands::kTimeoutCheckUpdate / 10);
91+
if (cml_data_.check_upd &&
92+
strcmp(CORTEX_CPP_VERSION, "default_version") != 0) {
93+
// TODO(sang) find a better way to handle
94+
// This is an extremely ugly way to deal with connection
95+
// hang when network down
96+
std::atomic<bool> done = false;
97+
std::thread t([&]() {
98+
if (auto latest_version =
99+
commands::CheckNewUpdate(commands::kTimeoutCheckUpdate);
100+
latest_version.has_value() && *latest_version != CORTEX_CPP_VERSION) {
101+
CLI_LOG("\nNew Cortex release available: "
102+
<< CORTEX_CPP_VERSION << " -> " << *latest_version);
103+
CLI_LOG("To update, run: " << commands::GetRole()
104+
<< commands::GetCortexBinary() << " update");
115105
}
106+
done = true;
107+
});
108+
// Do not wait for http connection timeout
109+
t.detach();
110+
int retry = 10;
111+
while (!done && retry--) {
112+
std::this_thread::sleep_for(commands::kTimeoutCheckUpdate / 10);
116113
}
117114
}
118115
#endif

engine/cli/main.cc

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,76 @@ int main(int argc, char* argv[]) {
102102
RemoveBinaryTempFileIfExists();
103103

104104
std::thread t1([]() {
105-
try {
105+
// TODO: namh current we only check for llamacpp. Need to add support for other engine
106+
auto should_check_for_latest_llamacpp_version = true;
107+
auto now = std::chrono::system_clock::now();
108+
109+
// read the yaml to see the last time we check for update
110+
auto config = file_manager_utils::GetCortexConfig();
111+
if (config.checkedForLlamacppUpdateAt != 0) {
112+
// if it passed a day, then we should check
113+
auto last_check =
114+
std::chrono::system_clock::time_point(
115+
std::chrono::milliseconds(config.checkedForLlamacppUpdateAt)) +
116+
std::chrono::hours(24);
117+
should_check_for_latest_llamacpp_version = now > last_check;
118+
CTL_DBG("should_check_for_latest_llamacpp_version: "
119+
<< should_check_for_latest_llamacpp_version);
120+
}
106121

107-
auto get_latest_llamacpp_version = []() {
122+
auto get_latest_version = []() -> cpp::result<std::string, std::string> {
123+
try {
108124
auto res = github_release_utils::GetReleaseByVersion(
109125
"janhq", "cortex.llamacpp", "latest");
110126
if (res.has_error()) {
111127
CTL_ERR("Failed to get latest llama.cpp version: " << res.error());
112-
return;
128+
return cpp::fail("Failed to get latest llama.cpp version: " +
129+
res.error());
113130
}
114131
CTL_INF("Latest llamacpp version: " << res->tag_name);
115-
};
132+
return res->tag_name;
133+
} catch (const std::exception& e) {
134+
CTL_ERR("Failed to get latest llama.cpp version: " << e.what());
135+
return cpp::fail("Failed to get latest llama.cpp version: " +
136+
std::string(e.what()));
137+
}
138+
};
139+
140+
if (should_check_for_latest_llamacpp_version) {
141+
auto res = get_latest_version();
142+
if (res.has_error()) {
143+
CTL_ERR("Failed to get latest llama.cpp version: " << res.error());
144+
return;
145+
}
146+
147+
CTL_DBG("latest llama.cpp version: " << res.value());
148+
config.checkedForLlamacppUpdateAt =
149+
std::chrono::duration_cast<std::chrono::milliseconds>(
150+
now.time_since_epoch())
151+
.count();
152+
config.latestLlamacppRelease = res.value();
153+
154+
auto upd_config_res = config_yaml_utils::DumpYamlConfig(
155+
config, file_manager_utils::GetConfigurationPath().string());
156+
if (upd_config_res.has_error()) {
157+
CTL_ERR("Failed to update config file: " << upd_config_res.error());
158+
} else {
159+
CTL_INF("Updated config file with latest llama.cpp version: "
160+
<< res.value());
161+
}
162+
}
116163

117-
get_latest_llamacpp_version();
118-
} catch (const std::exception& e) {
119-
CTL_ERR("Failed to get latest llama.cpp version: " << e.what());
164+
CTL_DBG("latest llama.cpp version: " << config.latestLlamacppRelease);
165+
CTL_DBG("llamacpp version: " << config.llamacppVersion);
166+
if (config.llamacppVersion.empty()) {
167+
return;
168+
}
169+
170+
if (config.latestLlamacppRelease != config.llamacppVersion) {
171+
CLI_LOG(
172+
"New llama.cpp version available: " << config.latestLlamacppRelease);
173+
CLI_LOG("To update, run: " << commands::GetCortexBinary()
174+
<< " engines update llama-cpp");
120175
}
121176
});
122177
t1.detach();

engine/test/components/test_cortex_config.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CortexConfigTest : public ::testing::Test {
1818
kDefaultHost,
1919
kDefaultPort,
2020
kDefaultCheckedForUpdateAt,
21+
kDefaultCheckedForLlamacppUpdateAt,
2122
kDefaultLatestRelease};
2223
}
2324

@@ -39,6 +40,7 @@ TEST_F(CortexConfigTest, DumpYamlConfig_WritesCorrectly) {
3940
"localhost",
4041
"8080",
4142
123456789,
43+
123456789,
4244
"v1.0.0"};
4345

4446
auto result = DumpYamlConfig(config, test_file_path);
@@ -67,6 +69,7 @@ TEST_F(CortexConfigTest, FromYaml_ReadsCorrectly) {
6769
"localhost",
6870
"8080",
6971
123456789,
72+
123456789,
7073
"v1.0.0"};
7174

7275
auto result = DumpYamlConfig(config, test_file_path);

engine/utils/config_yaml_utils.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ struct CortexConfig {
1919
std::string apiServerHost;
2020
std::string apiServerPort;
2121
uint64_t checkedForUpdateAt;
22+
uint64_t checkedForLlamacppUpdateAt;
2223
std::string latestRelease;
2324

25+
std::string latestLlamacppRelease;
2426
std::string huggingFaceToken;
2527
/**
2628
* Github's API requires a user-agent string.
@@ -38,7 +40,9 @@ const std::string kDefaultHost{"127.0.0.1"};
3840
const std::string kDefaultPort{"39281"};
3941
const int kDefaultMaxLines{100000};
4042
constexpr const uint64_t kDefaultCheckedForUpdateAt = 0u;
43+
constexpr const uint64_t kDefaultCheckedForLlamacppUpdateAt = 0u;
4144
constexpr const auto kDefaultLatestRelease = "default_version";
45+
constexpr const auto kDefaultLatestLlamacppRelease = "";
4246
constexpr const auto kDefaultCorsEnabled = true;
4347
const std::vector<std::string> kDefaultEnabledOrigins{};
4448

@@ -61,7 +65,9 @@ inline cpp::result<void, std::string> DumpYamlConfig(const CortexConfig& config,
6165
node["apiServerHost"] = config.apiServerHost;
6266
node["apiServerPort"] = config.apiServerPort;
6367
node["checkedForUpdateAt"] = config.checkedForUpdateAt;
68+
node["checkedForLlamacppUpdateAt"] = config.checkedForLlamacppUpdateAt;
6469
node["latestRelease"] = config.latestRelease;
70+
node["latestLlamacppRelease"] = config.latestLlamacppRelease;
6571
node["huggingFaceToken"] = config.huggingFaceToken;
6672
node["gitHubUserAgent"] = config.gitHubUserAgent;
6773
node["gitHubToken"] = config.gitHubToken;
@@ -92,7 +98,8 @@ inline CortexConfig FromYaml(const std::string& path,
9298
(!node["logFolderPath"] || !node["dataFolderPath"] ||
9399
!node["maxLogLines"] || !node["apiServerHost"] ||
94100
!node["apiServerPort"] || !node["checkedForUpdateAt"] ||
95-
!node["latestRelease"] || !node["logLlamaCppPath"] ||
101+
!node["checkedForLlamacppUpdateAt"] || !node["latestRelease"] ||
102+
!node["latestLlamacppRelease"] || !node["logLlamaCppPath"] ||
96103
!node["logOnnxPath"] || !node["logTensorrtLLMPath"] ||
97104
!node["huggingFaceToken"] || !node["gitHubUserAgent"] ||
98105
!node["gitHubToken"] || !node["llamacppVariant"] ||
@@ -126,9 +133,17 @@ inline CortexConfig FromYaml(const std::string& path,
126133
.checkedForUpdateAt = node["checkedForUpdateAt"]
127134
? node["checkedForUpdateAt"].as<uint64_t>()
128135
: default_cfg.checkedForUpdateAt,
136+
.checkedForLlamacppUpdateAt =
137+
node["checkedForLlamacppUpdateAt"]
138+
? node["checkedForLlamacppUpdateAt"].as<uint64_t>()
139+
: default_cfg.checkedForLlamacppUpdateAt,
129140
.latestRelease = node["latestRelease"]
130141
? node["latestRelease"].as<std::string>()
131142
: default_cfg.latestRelease,
143+
.latestLlamacppRelease =
144+
node["latestLlamacppRelease"]
145+
? node["latestLlamacppRelease"].as<std::string>()
146+
: default_cfg.latestLlamacppRelease,
132147
.huggingFaceToken = node["huggingFaceToken"]
133148
? node["huggingFaceToken"].as<std::string>()
134149
: "",

engine/utils/file_manager_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ inline config_yaml_utils::CortexConfig GetCortexConfig() {
195195
.apiServerHost = config_yaml_utils::kDefaultHost,
196196
.apiServerPort = config_yaml_utils::kDefaultPort,
197197
.checkedForUpdateAt = config_yaml_utils::kDefaultCheckedForUpdateAt,
198+
.checkedForLlamacppUpdateAt =
199+
config_yaml_utils::kDefaultCheckedForLlamacppUpdateAt,
198200
.latestRelease = config_yaml_utils::kDefaultLatestRelease,
201+
.latestLlamacppRelease = config_yaml_utils::kDefaultLatestLlamacppRelease,
199202
.enableCors = config_yaml_utils::kDefaultCorsEnabled,
200203
.allowedOrigins = config_yaml_utils::kDefaultEnabledOrigins};
201204

engine/utils/github_release_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "utils/curl_utils.h"
55
#include "utils/engine_constants.h"
66
#include "utils/engine_matcher_utils.h"
7+
#include "utils/logging_utils.h"
78
#include "utils/result.hpp"
89
#include "utils/url_parser.h"
910

@@ -193,7 +194,7 @@ inline cpp::result<GitHubRelease, std::string> GetReleaseByVersion(
193194
.pathParams = path_params,
194195
};
195196

196-
CTL_INF("GetReleaseByVersion: " << url.ToFullPath());
197+
CTL_DBG("GetReleaseByVersion: " << url.ToFullPath());
197198
auto result =
198199
curl_utils::SimpleGetJson(url_parser::FromUrl(url), kCurlGetTimeout);
199200

0 commit comments

Comments
 (0)