This repository was archived by the owner on Jul 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
feat: nightly updater #1175
Merged
Merged
feat: nightly updater #1175
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
88ca51d
feat: nightly updater
vansangpfiev 565c673
chore: add unit tests
vansangpfiev 164f9ea
Merge branch 'dev' of github.com:janhq/nitro into feat/nightly-updater
vansangpfiev c107bc9
fix: clean resources unit tests
vansangpfiev c55c167
fix: stop server before updating
vansangpfiev 69d52b2
fix: remove TODOs
vansangpfiev 1d3a54c
fix: use temp directory for cortex_temp binary
vansangpfiev 093146b
fix: comments
vansangpfiev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,124 @@ | ||
| #pragma once | ||
| #include <string> | ||
| #include <optional> | ||
| #include <string> | ||
|
|
||
| #include "httplib.h" | ||
| #include "nlohmann/json.hpp" | ||
| #include "utils/file_manager_utils.h" | ||
| #include "utils/logging_utils.h" | ||
|
|
||
| namespace commands { | ||
| #ifndef CORTEX_VARIANT | ||
| #define CORTEX_VARIANT file_manager_utils::kProdVariant | ||
| #endif | ||
| constexpr const auto kNightlyHost = "https://delta.jan.ai"; | ||
| constexpr const auto kNightlyFileName = "cortex-nightly.tar.gz"; | ||
vansangpfiev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const std::string kCortexBinary = "cortex"; | ||
|
|
||
| inline std::string GetCortexBinary() { | ||
| #if defined(_WIN32) | ||
| constexpr const bool has_exe = true; | ||
| #else | ||
| constexpr const bool has_exe = false; | ||
| #endif | ||
| if (CORTEX_VARIANT == file_manager_utils::kNightlyVariant) { | ||
vansangpfiev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return has_exe ? kCortexBinary + "-nightly.exe" | ||
| : kCortexBinary + "-nightly"; | ||
| } else if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) { | ||
| return has_exe ? kCortexBinary + "-beta.exe" : kCortexBinary + "-beta"; | ||
| } else { | ||
| return has_exe ? kCortexBinary + ".exe" : kCortexBinary; | ||
| } | ||
| } | ||
|
|
||
| inline std::string GetHostName() { | ||
| if (CORTEX_VARIANT == file_manager_utils::kNightlyVariant) { | ||
vansangpfiev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return "https://delta.jan.ai"; | ||
| } else { | ||
| return "https://api.github.com"; | ||
| } | ||
| } | ||
|
|
||
| inline std::string GetReleasePath() { | ||
| if (CORTEX_VARIANT == file_manager_utils::kNightlyVariant) { | ||
| return "/cortex/latest/version.json"; | ||
| } else { | ||
| return "/repos/janhq/cortex.cpp/releases/latest"; | ||
| } | ||
| } | ||
|
|
||
| inline void CheckNewUpdate() { | ||
| auto host_name = GetHostName(); | ||
| auto release_path = GetReleasePath(); | ||
| CTL_INF("Engine release path: " << host_name << release_path); | ||
|
|
||
| class CortexUpdCmd{ | ||
| httplib::Client cli(host_name); | ||
| if (auto res = cli.Get(release_path)) { | ||
| if (res->status == httplib::StatusCode::OK_200) { | ||
| try { | ||
| auto json_res = nlohmann::json::parse(res->body); | ||
| std::string latest_version = json_res["tag_name"].get<std::string>(); | ||
| std::string current_version = CORTEX_CPP_VERSION; | ||
| if (current_version != latest_version) { | ||
| CLI_LOG("\nA new release of cortex is available: " | ||
| << current_version << " -> " << latest_version); | ||
| CLI_LOG("To upgrade, run: cortex update"); | ||
| // CLI_LOG(json_res["html_url"].get<std::string>()); | ||
| } | ||
| } catch (const nlohmann::json::parse_error& e) { | ||
| CTL_INF("JSON parse error: " << e.what()); | ||
| } | ||
| } else { | ||
| CTL_INF("HTTP error: " << res->status); | ||
| } | ||
| } else { | ||
| auto err = res.error(); | ||
| CTL_INF("HTTP error: " << httplib::to_string(err)); | ||
| } | ||
| } | ||
|
|
||
| inline bool ReplaceBinaryInflight(const std::filesystem::path& src, | ||
| const std::filesystem::path& dst) { | ||
| if (src == dst) { | ||
| // Already has the newest | ||
| return true; | ||
| } | ||
| std::filesystem::path temp = std::filesystem::temp_directory_path() / "cortex_temp"; | ||
|
|
||
| try { | ||
| if (std::filesystem::exists(temp)) { | ||
| std::filesystem::remove(temp); | ||
| } | ||
|
|
||
| std::rename(dst.string().c_str(), temp.string().c_str()); | ||
| std::filesystem::copy_file( | ||
| src, dst, std::filesystem::copy_options::overwrite_existing); | ||
| std::filesystem::permissions(dst, std::filesystem::perms::owner_all | | ||
| std::filesystem::perms::group_all | | ||
| std::filesystem::perms::others_read | | ||
| std::filesystem::perms::others_exec); | ||
| auto download_folder = src.parent_path(); | ||
| std::filesystem::remove_all(download_folder); | ||
| } catch (const std::exception& e) { | ||
| CTL_ERR("Something wrong happened: " << e.what()); | ||
| if (std::filesystem::exists(temp)) { | ||
| std::rename(temp.string().c_str(), dst.string().c_str()); | ||
| CLI_LOG("Restored binary file"); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| class CortexUpdCmd { | ||
| public: | ||
| CortexUpdCmd(); | ||
| void Exec(std::string version); | ||
|
|
||
| private: | ||
| bool GetStableAndBeta(const std::string& v); | ||
| bool GetNightly(const std::string& v); | ||
| }; | ||
|
|
||
| } // namespace commands | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.