From bdef898fa20f9ff2e68f1548f1c93b81d0e5a75e Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 13:15:34 +0700 Subject: [PATCH 01/11] feat: beta updater --- engine/CMakeLists.txt | 118 ++++-------------- engine/commands/cortex_upd_cmd.cc | 197 +++++++++++++++++++++--------- engine/commands/cortex_upd_cmd.h | 42 ++++++- 3 files changed, 197 insertions(+), 160 deletions(-) diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 58661c508..f70d8e1ee 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -2,18 +2,6 @@ cmake_minimum_required(VERSION 3.5) project(cortex C CXX) -# Build using CMAKE-JS -if(DEFINED CMAKE_JS_INC) - if(WIN32) - add_definitions( - -DV8_COMPRESS_POINTERS - -DV8_REVERSE_JSARGS - -DV8_COMPRESS_POINTERS_IN_ISOLATE_CAGE - ) - endif() - include_directories(${CMAKE_JS_INC}) -endif() - include(CheckIncludeFileCXX) check_include_file_cxx(any HAS_ANY) @@ -33,8 +21,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) set(OPENSSL_USE_STATIC_LIBS TRUE) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -# set(CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/build-deps/_install) -# This is the critical line for installing another package if(MSVC) add_compile_options( @@ -43,16 +29,6 @@ if(MSVC) $<$:/MT> #--| ) endif() - -if(LLAMA_CUDA) - cmake_minimum_required(VERSION 3.17) - - find_package(CUDAToolkit) - if(CUDAToolkit_FOUND) - message(STATUS "cuBLAS found") - add_compile_definitions(GGML_USE_CUDA) - endif() -endif() if(DEBUG) message(STATUS "CORTEX-CPP DEBUG IS ON") @@ -63,6 +39,14 @@ if(NOT DEFINED CORTEX_VARIANT) set(CORTEX_VARIANT "prod") endif() +if(CORTEX_VARIANT STREQUAL "beta") + set(TARGET_NAME "${PROJECT_NAME}-beta") +elseif(CORTEX_VARIANT STREQUAL "nightly") + set(TARGET_NAME "${PROJECT_NAME}-nightly") +else() + set(TARGET_NAME ${PROJECT_NAME}) +endif() + if(NOT DEFINED CORTEX_CONFIG_FILE_PATH) set(CORTEX_CONFIG_FILE_PATH "user_home") endif() @@ -70,22 +54,6 @@ endif() if(NOT DEFINED CORTEX_CPP_VERSION) set(CORTEX_CPP_VERSION "default_version") endif() - -if(APPLE) - if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm.*|ARM64)$") - # MacOS silicon - set(LLAMA_METAL_EMBED_LIBRARY ON) - set(WHISPER_COREML 1) - else() - # MacOS amd64 - set(LLAMA_METAL OFF) - endif() -endif() - -if(DEFINED CMAKE_JS_INC) - # define NPI_VERSION - add_compile_definitions(NAPI_VERSION=8) -endif() add_compile_definitions(CORTEX_VARIANT="${CORTEX_VARIANT}") add_compile_definitions(CORTEX_CPP_VERSION="${CORTEX_CPP_VERSION}") @@ -107,72 +75,28 @@ find_package(unofficial-minizip CONFIG REQUIRED) find_package(LibArchive REQUIRED) find_package(tabulate CONFIG REQUIRED) -# Build using CMAKE-JS -if(DEFINED CMAKE_JS_INC) - if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") - endif() - - add_library(${PROJECT_NAME} SHARED addon.cc +add_executable(${TARGET_NAME} main.cc ${CMAKE_CURRENT_SOURCE_DIR}/utils/cpuid/cpu_info.cc ${CMAKE_CURRENT_SOURCE_DIR}/utils/file_logger.cc - ${CMAKE_JS_SRC} ) - if(WIN32) - target_link_libraries(${PROJECT_NAME} - PRIVATE - msvcprt.lib - msvcrt.lib - vcruntime.lib - ucrt.lib - ${CMAKE_JS_LIB} - ) - endif() -else() # Official build - add_executable(${PROJECT_NAME} main.cc - ${CMAKE_CURRENT_SOURCE_DIR}/utils/cpuid/cpu_info.cc - ${CMAKE_CURRENT_SOURCE_DIR}/utils/file_logger.cc - ) -endif() - -# ############################################################################## -# If you include the drogon source code locally in your project, use this method -# to add drogon add_subdirectory(cortex-cpp-deps) -# target_link_libraries(${PROJECT_NAME} PRIVATE cortex-cpp-deps) -# -# and comment out the following lines - -target_link_libraries(${PROJECT_NAME} PRIVATE httplib::httplib) -target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json) -target_link_libraries(${PROJECT_NAME} PRIVATE jinja2cpp) -target_link_libraries(${PROJECT_NAME} PRIVATE CLI11::CLI11) -target_link_libraries(${PROJECT_NAME} PRIVATE unofficial::minizip::minizip) -target_link_libraries(${PROJECT_NAME} PRIVATE LibArchive::LibArchive) -target_link_libraries(${PROJECT_NAME} PRIVATE tabulate::tabulate) - -# Build using CMAKE-JS -if(DEFINED CMAKE_JS_INC) - set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") - - target_link_libraries(${PROJECT_NAME} PRIVATE Drogon::Drogon - ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_JS_LIB}) - - if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET) - # Generate node.lib - execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) - endif() -else() - target_link_libraries(${PROJECT_NAME} PRIVATE JsonCpp::JsonCpp Drogon::Drogon OpenSSL::SSL OpenSSL::Crypto yaml-cpp::yaml-cpp +target_link_libraries(${TARGET_NAME} PRIVATE httplib::httplib) +target_link_libraries(${TARGET_NAME} PRIVATE nlohmann_json::nlohmann_json) +target_link_libraries(${TARGET_NAME} PRIVATE jinja2cpp) +target_link_libraries(${TARGET_NAME} PRIVATE CLI11::CLI11) +target_link_libraries(${TARGET_NAME} PRIVATE unofficial::minizip::minizip) +target_link_libraries(${TARGET_NAME} PRIVATE LibArchive::LibArchive) +target_link_libraries(${TARGET_NAME} PRIVATE tabulate::tabulate) +target_link_libraries(${TARGET_NAME} PRIVATE JsonCpp::JsonCpp Drogon::Drogon OpenSSL::SSL OpenSSL::Crypto yaml-cpp::yaml-cpp ${CMAKE_THREAD_LIBS_INIT}) -endif() + # ############################################################################## if(CMAKE_CXX_STANDARD LESS 17) # With C++14, use boost to support any and std::string_view message(STATUS "use c++14") find_package(Boost 1.61.0 REQUIRED) - target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS}) + target_include_directories(${TARGET_NAME} PRIVATE ${Boost_INCLUDE_DIRS}) elseif(CMAKE_CXX_STANDARD LESS 20) message(STATUS "use c++17") else() @@ -187,6 +111,6 @@ aux_source_directory(cortex-common CORTEX_COMMON) aux_source_directory(config CONFIG_SRC) aux_source_directory(commands COMMANDS_SRC) -target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ) +target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ) -target_sources(${PROJECT_NAME} PRIVATE ${COMMANDS_SRC} ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC}) +target_sources(${TARGET_NAME} PRIVATE ${COMMANDS_SRC} ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC}) diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index a4343e55c..28d999cc2 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -27,17 +27,20 @@ void CortexUpdCmd::Exec(std::string v) { ssc.Exec(); } } - if (CORTEX_VARIANT == file_manager_utils::kNightlyVariant) { - if (!GetNightly(v)) + if (CORTEX_VARIANT == file_manager_utils::kProdVariant) { + if (!GetStable(v)) + return; + } else if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) { + if (!GetBeta(v)) return; } else { - if (!GetStableAndBeta(v)) + if (!GetNightly(v)) return; } CLI_LOG("Update cortex sucessfully"); } -bool CortexUpdCmd::GetStableAndBeta(const std::string& v) { +bool CortexUpdCmd::GetStable(const std::string& v) { // Check if the architecture and OS are supported auto system_info = system_info_utils::GetSystemInfo(); if (system_info.arch == system_info_utils::kUnsupported || @@ -61,69 +64,82 @@ bool CortexUpdCmd::GetStableAndBeta(const std::string& v) { if (auto res = cli.Get(release_path.str())) { if (res->status == httplib::StatusCode::OK_200) { try { - auto jsonResponse = nlohmann::json::parse(res->body); - auto assets = jsonResponse["assets"]; - auto os_arch{system_info.os + "-" + system_info.arch}; - - std::string matched_variant = ""; - for (auto& asset : assets) { - auto asset_name = asset["name"].get(); - if (asset_name.find(kCortexBinary) != std::string::npos && - asset_name.find(os_arch) != std::string::npos) { - matched_variant = asset_name; - break; - } - CTL_INF(asset_name); + auto json_data = nlohmann::json::parse(res->body); + if (json_data.empty()) { + CLI_LOG("Version not found: " << v); + return false; } - if (matched_variant.empty()) { - CTL_ERR("No variant found for " << os_arch); + + if (!HandleGithubRelease(json_data["assets"], + {system_info.os + "-" + system_info.arch})) { return false; } - CTL_INF("Matched variant: " << matched_variant); - - for (auto& asset : assets) { - auto asset_name = asset["name"].get(); - if (asset_name == matched_variant) { - std::string host{"https://github.com"}; - - auto full_url = asset["browser_download_url"].get(); - std::string path = full_url.substr(host.length()); - - auto fileName = asset["name"].get(); - CTL_INF("URL: " << full_url); - - auto download_task = DownloadTask{.id = "cortex", - .type = DownloadType::Cortex, - .error = std::nullopt, - .items = {DownloadItem{ - .id = "cortex", - .host = host, - .fileName = fileName, - .type = DownloadType::Cortex, - .path = path, - }}}; - - DownloadService download_service; - download_service.AddDownloadTask( - download_task, - [this](const std::string& absolute_path, bool unused) { - // try to unzip the downloaded file - std::filesystem::path download_path{absolute_path}; - CTL_INF("Downloaded engine path: " << download_path.string()); - - std::filesystem::path extract_path = - download_path.parent_path().parent_path(); - - archive_utils::ExtractArchive(download_path.string(), - extract_path.string()); - - CTL_INF("Finished!"); - }); + } catch (const nlohmann::json::parse_error& e) { + CTL_ERR("JSON parse error: " << e.what()); + return false; + } + } else { + CTL_ERR("HTTP error: " << res->status); + return false; + } + } else { + auto err = res.error(); + CTL_ERR("HTTP error: " << httplib::to_string(err)); + return false; + } + + // Replace binary file + auto executable_path = file_manager_utils::GetExecutableFolderContainerPath(); + auto src = executable_path / "cortex" / kCortexBinary / GetCortexBinary(); + auto dst = executable_path / GetCortexBinary(); + return ReplaceBinaryInflight(src, dst); +} + +bool CortexUpdCmd::GetBeta(const std::string& v) { + // Check if the architecture and OS are supported + auto system_info = system_info_utils::GetSystemInfo(); + if (system_info.arch == system_info_utils::kUnsupported || + system_info.os == system_info_utils::kUnsupported) { + CTL_ERR("Unsupported OS or architecture: " << system_info.os << ", " + << system_info.arch); + return false; + } + CTL_INF("OS: " << system_info.os << ", Arch: " << system_info.arch); + + // Download file + constexpr auto github_host = "https://api.github.com"; + std::ostringstream release_path; + release_path << "/repos/janhq/cortex.cpp/releases"; + CTL_INF("Engine release path: " << github_host << release_path.str()); + + httplib::Client cli(github_host); + if (auto res = cli.Get(release_path.str())) { + if (res->status == httplib::StatusCode::OK_200) { + try { + auto json_res = nlohmann::json::parse(res->body); + + nlohmann::json json_data; + for (auto& jr : json_res) { + // Get the latest beta or match version + if (auto tag = jr["tag_name"].get(); + (v.empty() && tag.find(kBetaComp) != std::string::npos) || + (tag == v)) { + json_data = jr; break; } } + + if (json_data.empty()) { + CLI_LOG("Version not found: " << v); + return false; + } + + if (!HandleGithubRelease(json_data["assets"], + {system_info.os + "-" + system_info.arch})) { + return false; + } } catch (const nlohmann::json::parse_error& e) { - std::cerr << "JSON parse error: " << e.what() << std::endl; + CTL_ERR("JSON parse error: " << e.what()); return false; } } else { @@ -143,6 +159,67 @@ bool CortexUpdCmd::GetStableAndBeta(const std::string& v) { return ReplaceBinaryInflight(src, dst); } +bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets, + const std::string& os_arch) { + std::string matched_variant = ""; + for (auto& asset : assets) { + auto asset_name = asset["name"].get(); + if (asset_name.find(kCortexBinary) != std::string::npos && + asset_name.find(os_arch) != std::string::npos) { + matched_variant = asset_name; + break; + } + CTL_INF(asset_name); + } + if (matched_variant.empty()) { + CTL_ERR("No variant found for " << os_arch); + return false; + } + CTL_INF("Matched variant: " << matched_variant); + + for (auto& asset : assets) { + auto asset_name = asset["name"].get(); + if (asset_name == matched_variant) { + std::string host{"https://github.com"}; + + auto full_url = asset["browser_download_url"].get(); + std::string path = full_url.substr(host.length()); + + auto fileName = asset["name"].get(); + CTL_INF("URL: " << full_url); + + auto download_task = DownloadTask{.id = "cortex", + .type = DownloadType::Cortex, + .error = std::nullopt, + .items = {DownloadItem{ + .id = "cortex", + .host = host, + .fileName = fileName, + .type = DownloadType::Cortex, + .path = path, + }}}; + + DownloadService download_service; + download_service.AddDownloadTask( + download_task, [this](const std::string& absolute_path, bool unused) { + // try to unzip the downloaded file + std::filesystem::path download_path{absolute_path}; + CTL_INF("Downloaded engine path: " << download_path.string()); + + std::filesystem::path extract_path = + download_path.parent_path().parent_path(); + + archive_utils::ExtractArchive(download_path.string(), + extract_path.string()); + + CTL_INF("Finished!"); + }); + break; + } + } + return true; +} + bool CortexUpdCmd::GetNightly(const std::string& v) { // Check if the architecture and OS are supported auto system_info = system_info_utils::GetSystemInfo(); diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index 396c6cdaf..ac3c5fb04 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -14,6 +14,7 @@ namespace commands { constexpr const auto kNightlyHost = "https://delta.jan.ai"; constexpr const auto kNightlyFileName = "cortex-nightly.tar.gz"; const std::string kCortexBinary = "cortex"; +constexpr const auto kBetaComp = "-rc"; inline std::string GetCortexBinary() { #if defined(_WIN32) @@ -42,6 +43,8 @@ inline std::string GetHostName() { inline std::string GetReleasePath() { if (CORTEX_VARIANT == file_manager_utils::kNightlyVariant) { return "/cortex/latest/version.json"; + } else if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) { + return "/repos/janhq/cortex.cpp/releases"; } else { return "/repos/janhq/cortex.cpp/releases/latest"; } @@ -56,8 +59,31 @@ inline void CheckNewUpdate() { if (auto res = cli.Get(release_path)) { if (res->status == httplib::StatusCode::OK_200) { try { + auto get_latest = [](const nlohmann::json& data) -> std::string { + if (data.empty()) { + return ""; + } + + if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) { + for (auto& d : data) { + if (auto tag = d["tag_name"].get(); + tag.find(kBetaComp) != std::string::npos) { + return tag; + } + } + return data[0]["tag_name"].get(); + } else { + return data["tag_name"].get(); + } + return ""; + }; + auto json_res = nlohmann::json::parse(res->body); - std::string latest_version = json_res["tag_name"].get(); + std::string latest_version = get_latest(json_res); + if(latest_version.empty()) { + CTL_WRN("Release not found!"); + return; + } std::string current_version = CORTEX_CPP_VERSION; if (current_version != latest_version) { CLI_LOG("\nA new release of cortex is available: " @@ -83,7 +109,8 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, // Already has the newest return true; } - std::filesystem::path temp = std::filesystem::temp_directory_path() / "cortex_temp"; + std::filesystem::path temp = + std::filesystem::temp_directory_path() / "cortex_temp"; try { if (std::filesystem::exists(temp)) { @@ -111,13 +138,22 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, return true; } + +// This class manages the 'cortex update' command functionality +// There are three release types available: +// - Stable: Only retrieves the latest version +// - Beta: Allows retrieval of the latest beta version and specific versions using the -v flag +// - Nightly: Enables retrieval of the latest nightly build and specific versions using the -v flag class CortexUpdCmd { public: CortexUpdCmd(); void Exec(std::string version); private: - bool GetStableAndBeta(const std::string& v); + bool GetStable(const std::string& v); + bool GetBeta(const std::string& v); + bool HandleGithubRelease(const nlohmann::json& assets, + const std::string& os_arch); bool GetNightly(const std::string& v); }; From 6dd0209489eaa0b603d912df3eac914196f4e9c6 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 13:34:53 +0700 Subject: [PATCH 02/11] fix: add --version --- engine/controllers/command_line_parser.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index 87f594841..d9accfc47 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -186,7 +186,7 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) { CLI_LOG("default"); #endif }; - app_.add_flag_function("-v", cb, "Cortex version"); + app_.add_flag_function("-v,--version", cb, "Cortex version"); std::string cortex_version; bool check_update = true; From e821248a6da8cf57995b47aff5daca49d89ea8bc Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 13:55:24 +0700 Subject: [PATCH 03/11] fix: more checks --- engine/Makefile | 2 +- engine/commands/cortex_upd_cmd.cc | 3 ++- engine/commands/cortex_upd_cmd.h | 12 +++++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/engine/Makefile b/engine/Makefile index 9f72f8355..3443d9f86 100644 --- a/engine/Makefile +++ b/engine/Makefile @@ -56,7 +56,7 @@ endif pre-package: ifeq ($(OS),Windows_NT) @powershell -Command "mkdir -p cortex;" - @powershell -Command "cp build\cortex.exe .\cortex\;" + @powershell -Command "cp build\*.exe .\cortex\;" @powershell -Command "cp ..\.github\patches\windows\msvcp140.dll .\cortex\;" @powershell -Command "cp ..\.github\patches\windows\vcruntime140_1.dll .\cortex\;" @powershell -Command "cp ..\.github\patches\windows\vcruntime140.dll .\cortex\;" diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index 28d999cc2..87f1df8dc 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -165,7 +165,8 @@ bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets, for (auto& asset : assets) { auto asset_name = asset["name"].get(); if (asset_name.find(kCortexBinary) != std::string::npos && - asset_name.find(os_arch) != std::string::npos) { + asset_name.find(os_arch) != std::string::npos && + asset_name.find(kReleaseFormat) != std::string::npos) { matched_variant = asset_name; break; } diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index ac3c5fb04..c242f41b1 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -15,6 +15,7 @@ constexpr const auto kNightlyHost = "https://delta.jan.ai"; constexpr const auto kNightlyFileName = "cortex-nightly.tar.gz"; const std::string kCortexBinary = "cortex"; constexpr const auto kBetaComp = "-rc"; +constexpr const auto kReleaseFormat = ".tar.gz"; inline std::string GetCortexBinary() { #if defined(_WIN32) @@ -63,7 +64,7 @@ inline void CheckNewUpdate() { if (data.empty()) { return ""; } - + if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) { for (auto& d : data) { if (auto tag = d["tag_name"].get(); @@ -80,7 +81,7 @@ inline void CheckNewUpdate() { auto json_res = nlohmann::json::parse(res->body); std::string latest_version = get_latest(json_res); - if(latest_version.empty()) { + if (latest_version.empty()) { CTL_WRN("Release not found!"); return; } @@ -88,8 +89,10 @@ inline void CheckNewUpdate() { 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()); + CLI_LOG("To upgrade, run: " << GetCortexBinary() << " update"); + if (CORTEX_VARIANT != file_manager_utils::kNightlyVariant) { + CLI_LOG(json_res["html_url"].get()); + } } } catch (const nlohmann::json::parse_error& e) { CTL_INF("JSON parse error: " << e.what()); @@ -138,7 +141,6 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, return true; } - // This class manages the 'cortex update' command functionality // There are three release types available: // - Stable: Only retrieves the latest version From c297dc04ee224b223fca6c0a1b0a063f9dddcf36 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 14:02:11 +0700 Subject: [PATCH 04/11] fix: more --- engine/commands/cortex_upd_cmd.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index c242f41b1..ddd32ae74 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -112,9 +112,8 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, // Already has the newest return true; } - std::filesystem::path temp = - std::filesystem::temp_directory_path() / "cortex_temp"; - + auto temp = std::filesystem::temp_directory_path() / "cortex_temp"; + auto download_folder = src.parent_path(); try { if (std::filesystem::exists(temp)) { std::filesystem::remove(temp); @@ -127,7 +126,6 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, 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()); @@ -135,6 +133,9 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, std::rename(temp.string().c_str(), dst.string().c_str()); CLI_LOG("Restored binary file"); } + if (std::filesystem::exists(download_folder)) { + std::filesystem::remove_all(download_folder); + } return false; } From ab7d02d35745e6cf8f254a687cc6ae7fbe1bf651 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 14:31:13 +0700 Subject: [PATCH 05/11] fix: no target name changes --- engine/CMakeLists.txt | 8 +------- engine/Makefile | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index f70d8e1ee..ae1dc0cb0 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -39,13 +39,7 @@ if(NOT DEFINED CORTEX_VARIANT) set(CORTEX_VARIANT "prod") endif() -if(CORTEX_VARIANT STREQUAL "beta") - set(TARGET_NAME "${PROJECT_NAME}-beta") -elseif(CORTEX_VARIANT STREQUAL "nightly") - set(TARGET_NAME "${PROJECT_NAME}-nightly") -else() - set(TARGET_NAME ${PROJECT_NAME}) -endif() +set(TARGET_NAME ${PROJECT_NAME}) if(NOT DEFINED CORTEX_CONFIG_FILE_PATH) set(CORTEX_CONFIG_FILE_PATH "user_home") diff --git a/engine/Makefile b/engine/Makefile index 3443d9f86..9f72f8355 100644 --- a/engine/Makefile +++ b/engine/Makefile @@ -56,7 +56,7 @@ endif pre-package: ifeq ($(OS),Windows_NT) @powershell -Command "mkdir -p cortex;" - @powershell -Command "cp build\*.exe .\cortex\;" + @powershell -Command "cp build\cortex.exe .\cortex\;" @powershell -Command "cp ..\.github\patches\windows\msvcp140.dll .\cortex\;" @powershell -Command "cp ..\.github\patches\windows\vcruntime140_1.dll .\cortex\;" @powershell -Command "cp ..\.github\patches\windows\vcruntime140.dll .\cortex\;" From a16d9c1a132f536852f2ef22622aae7bffa71c4f Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 15:05:06 +0700 Subject: [PATCH 06/11] fix: download binary to temp folder for cortex update --- engine/commands/cortex_upd_cmd.cc | 6 ++++-- engine/commands/cortex_upd_cmd.h | 5 ----- engine/utils/file_manager_utils.h | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index 87f1df8dc..845b80f15 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -154,7 +154,8 @@ bool CortexUpdCmd::GetBeta(const std::string& v) { // Replace binary file auto executable_path = file_manager_utils::GetExecutableFolderContainerPath(); - auto src = executable_path / "cortex" / kCortexBinary / GetCortexBinary(); + auto src = std::filesystem::temp_directory_path() / "cortex" / kCortexBinary / + GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); return ReplaceBinaryInflight(src, dst); } @@ -268,7 +269,8 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { // Replace binay file auto executable_path = file_manager_utils::GetExecutableFolderContainerPath(); - auto src = executable_path / "cortex" / GetCortexBinary(); + auto src = + std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); return ReplaceBinaryInflight(src, dst); } diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index ddd32ae74..a16bbb5b4 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -113,7 +113,6 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, return true; } auto temp = std::filesystem::temp_directory_path() / "cortex_temp"; - auto download_folder = src.parent_path(); try { if (std::filesystem::exists(temp)) { std::filesystem::remove(temp); @@ -126,16 +125,12 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, std::filesystem::perms::group_all | std::filesystem::perms::others_read | std::filesystem::perms::others_exec); - 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"); } - if (std::filesystem::exists(download_folder)) { - std::filesystem::remove_all(download_folder); - } return false; } diff --git a/engine/utils/file_manager_utils.h b/engine/utils/file_manager_utils.h index b4d7ab07a..1e5c522e0 100644 --- a/engine/utils/file_manager_utils.h +++ b/engine/utils/file_manager_utils.h @@ -230,7 +230,7 @@ inline std::filesystem::path GetContainerFolderPath( } else if (type == "CudaToolkit") { container_folder_path = current_path; } else if (type == "Cortex") { - container_folder_path = current_path / "cortex"; + container_folder_path = std::filesystem::temp_directory_path() / "cortex"; } else { container_folder_path = current_path / "misc"; } From 94393a7821ad5dfa46f29fe139277c64c4f88cdb Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 15:42:38 +0700 Subject: [PATCH 07/11] fix: rename should be in the same folder --- engine/commands/cortex_upd_cmd.cc | 6 +++--- engine/commands/cortex_upd_cmd.h | 3 ++- engine/main.cc | 13 +++++++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index 845b80f15..a2d28c37c 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -238,7 +238,7 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { std::ostringstream release_path; release_path << "cortex/" << version << "/" << system_info.os << "-" << system_info.arch << "/" << kNightlyFileName; - CTL_INF("Engine release path: " << kNightlyHost << release_path.str()); + CTL_INF("Engine release path: " << kNightlyHost << "/" << release_path.str()); auto download_task = DownloadTask{.id = "cortex", .type = DownloadType::Cortex, @@ -269,8 +269,8 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { // Replace binay file auto executable_path = file_manager_utils::GetExecutableFolderContainerPath(); - auto src = - std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); + auto src = std::filesystem::temp_directory_path() / "cortex" / kCortexBinary / + GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); return ReplaceBinaryInflight(src, dst); } diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index a16bbb5b4..284027fda 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -112,7 +112,8 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, // Already has the newest return true; } - auto temp = std::filesystem::temp_directory_path() / "cortex_temp"; + auto temp = + file_manager_utils::GetExecutableFolderContainerPath() / "cortex_temp"; try { if (std::filesystem::exists(temp)) { std::filesystem::remove(temp); diff --git a/engine/main.cc b/engine/main.cc index 1450d887c..1cc143ed9 100644 --- a/engine/main.cc +++ b/engine/main.cc @@ -27,7 +27,8 @@ void RunServer() { auto config = file_manager_utils::GetCortexConfig(); - LOG_INFO << "Host: " << config.apiServerHost << " Port: " << config.apiServerPort << "\n"; + LOG_INFO << "Host: " << config.apiServerHost + << " Port: " << config.apiServerPort << "\n"; // Create logs/ folder and setup log to file std::filesystem::create_directory(config.logFolderPath + "/" + @@ -72,7 +73,8 @@ void RunServer() { LOG_INFO << "Server started, listening at: " << config.apiServerHost << ":" << config.apiServerPort; LOG_INFO << "Please load your model"; - drogon::app().addListener(config.apiServerHost, std::stoi(config.apiServerPort)); + drogon::app().addListener(config.apiServerHost, + std::stoi(config.apiServerPort)); drogon::app().setThreadNum(drogon_thread_num); LOG_INFO << "Number of thread is:" << drogon::app().getThreadNum(); @@ -131,6 +133,13 @@ void ForkProcess() { int main(int argc, char* argv[]) { { file_manager_utils::CreateConfigFileIfNotExist(); } + // Delete temporary file if it exists + auto temp = + file_manager_utils::GetExecutableFolderContainerPath() / "cortex_temp"; + if (std::filesystem::exists(temp)) { + std::filesystem::remove(temp); + } + // Check if this process is for python execution if (argc > 1) { if (strcmp(argv[1], "--run_python_file") == 0) { From 7d8956c445b11e17c10786ae6c0c0e124906298d Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 16:22:46 +0700 Subject: [PATCH 08/11] fix: comment --- engine/commands/cortex_upd_cmd.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index 284027fda..eda5eb9e5 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -90,9 +90,7 @@ inline void CheckNewUpdate() { CLI_LOG("\nA new release of cortex is available: " << current_version << " -> " << latest_version); CLI_LOG("To upgrade, run: " << GetCortexBinary() << " update"); - if (CORTEX_VARIANT != file_manager_utils::kNightlyVariant) { - CLI_LOG(json_res["html_url"].get()); - } + // CLI_LOG(json_res["html_url"].get()); } } catch (const nlohmann::json::parse_error& e) { CTL_INF("JSON parse error: " << e.what()); From 373d3c54308cedeb0576d87c9165a71f6c793c0d Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 16:25:53 +0700 Subject: [PATCH 09/11] fix: more --- engine/commands/cortex_upd_cmd.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index eda5eb9e5..9a2982271 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -90,7 +90,9 @@ inline void CheckNewUpdate() { CLI_LOG("\nA new release of cortex is available: " << current_version << " -> " << latest_version); CLI_LOG("To upgrade, run: " << GetCortexBinary() << " update"); - // CLI_LOG(json_res["html_url"].get()); + if (CORTEX_VARIANT == file_manager_utils::kProdVariant) { + CLI_LOG(json_res["html_url"].get()); + } } } catch (const nlohmann::json::parse_error& e) { CTL_INF("JSON parse error: " << e.what()); From 5076bf13ec2d7f568e63e418be83fc094544d3d9 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 16 Sep 2024 11:52:49 +0700 Subject: [PATCH 10/11] fix: use constants --- engine/commands/cortex_upd_cmd.cc | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index 7bba227ce..3877c3e4f 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -42,16 +42,12 @@ bool CortexUpdCmd::GetStable(const std::string& v) { CTL_INF("OS: " << system_info.os << ", Arch: " << system_info.arch); // Download file - constexpr auto github_host = "https://api.github.com"; - // std::string version = v.empty() ? "latest" : std::move(v); - // TODO(sang): support download with version - std::string version = "latest"; - std::ostringstream release_path; - release_path << "/repos/janhq/cortex.cpp/releases/" << version; - CTL_INF("Engine release path: " << github_host << release_path.str()); + auto github_host = GetHostName(); + auto release_path = GetReleasePath(); + CTL_INF("Engine release path: " << github_host << release_path); httplib::Client cli(github_host); - if (auto res = cli.Get(release_path.str())) { + if (auto res = cli.Get(release_path)) { if (res->status == httplib::StatusCode::OK_200) { try { auto json_data = nlohmann::json::parse(res->body); @@ -98,13 +94,12 @@ bool CortexUpdCmd::GetBeta(const std::string& v) { CTL_INF("OS: " << system_info.os << ", Arch: " << system_info.arch); // Download file - constexpr auto github_host = "https://api.github.com"; - std::ostringstream release_path; - release_path << "/repos/janhq/cortex.cpp/releases"; - CTL_INF("Engine release path: " << github_host << release_path.str()); + auto github_host = GetHostName(); + auto release_path = GetReleasePath(); + CTL_INF("Engine release path: " << github_host << release_path); httplib::Client cli(github_host); - if (auto res = cli.Get(release_path.str())) { + if (auto res = cli.Get(release_path)) { if (res->status == httplib::StatusCode::OK_200) { try { auto json_res = nlohmann::json::parse(res->body); @@ -177,8 +172,8 @@ bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets, auto file_name = asset["name"].get(); CTL_INF("Download url: " << download_url); - auto local_path = std::filesystem::temp_directory_path() / - "cortex" / asset_name; + auto local_path = + std::filesystem::temp_directory_path() / "cortex" / asset_name; auto download_task{DownloadTask{.id = "cortex", .type = DownloadType::Cortex, .items = {DownloadItem{ @@ -231,8 +226,7 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { CTL_INF("Engine release path: " << url_parser::FromUrl(url_obj)); std::filesystem::path localPath = - std::filesystem::temp_directory_path() / "cortex" / - path_list.back(); + std::filesystem::temp_directory_path() / "cortex" / path_list.back(); auto download_task = DownloadTask{.id = "cortex", .type = DownloadType::Cortex, From 9b87aa7e2a058786b6012916a0cd4df513148af0 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 16 Sep 2024 12:34:18 +0700 Subject: [PATCH 11/11] fix: comments --- engine/commands/cortex_upd_cmd.cc | 32 +++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index 3877c3e4f..7c2a1f423 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -37,7 +37,6 @@ void CortexUpdCmd::Exec(std::string v) { } bool CortexUpdCmd::GetStable(const std::string& v) { - // Check if the architecture and OS are supported auto system_info = system_info_utils::GetSystemInfo(); CTL_INF("OS: " << system_info.os << ", Arch: " << system_info.arch); @@ -83,14 +82,7 @@ bool CortexUpdCmd::GetStable(const std::string& v) { } bool CortexUpdCmd::GetBeta(const std::string& v) { - // Check if the architecture and OS are supported auto system_info = system_info_utils::GetSystemInfo(); - if (system_info.arch == system_info_utils::kUnsupported || - system_info.os == system_info_utils::kUnsupported) { - CTL_ERR("Unsupported OS or architecture: " << system_info.os << ", " - << system_info.arch); - return false; - } CTL_INF("OS: " << system_info.os << ", Arch: " << system_info.arch); // Download file @@ -140,8 +132,8 @@ bool CortexUpdCmd::GetBeta(const std::string& v) { // Replace binary file auto executable_path = file_manager_utils::GetExecutableFolderContainerPath(); - auto src = std::filesystem::temp_directory_path() / "cortex" / kCortexBinary / - GetCortexBinary(); + auto src = + std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); return ReplaceBinaryInflight(src, dst); } @@ -174,6 +166,14 @@ bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets, auto local_path = std::filesystem::temp_directory_path() / "cortex" / asset_name; + try { + if (!std::filesystem::exists(local_path.parent_path())) { + std::filesystem::create_directories(local_path.parent_path()); + } + } catch (const std::filesystem::filesystem_error& e) { + CTL_ERR("Failed to create directories: " << e.what()); + return false; + } auto download_task{DownloadTask{.id = "cortex", .type = DownloadType::Cortex, .items = {DownloadItem{ @@ -227,6 +227,14 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { std::filesystem::path localPath = std::filesystem::temp_directory_path() / "cortex" / path_list.back(); + try { + if (!std::filesystem::exists(localPath.parent_path())) { + std::filesystem::create_directories(localPath.parent_path()); + } + } catch (const std::filesystem::filesystem_error& e) { + CTL_ERR("Failed to create directories: " << e.what()); + return false; + } auto download_task = DownloadTask{.id = "cortex", .type = DownloadType::Cortex, @@ -253,8 +261,8 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { // Replace binary file auto executable_path = file_manager_utils::GetExecutableFolderContainerPath(); - auto src = std::filesystem::temp_directory_path() / "cortex" / kCortexBinary / - GetCortexBinary(); + auto src = + std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); return ReplaceBinaryInflight(src, dst); }