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

chore: Implement Normalize Engine and remove anonymous namespaces #2053

Merged
merged 4 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions engine/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ find_package(lfreist-hwinfo CONFIG REQUIRED)

add_executable(${TARGET_NAME} main.cc
${CMAKE_CURRENT_SOURCE_DIR}/../utils/cpuid/cpu_info.cc
${CMAKE_CURRENT_SOURCE_DIR}/../utils/normalize_engine.cc
${CMAKE_CURRENT_SOURCE_DIR}/../utils/file_logger.cc
${CMAKE_CURRENT_SOURCE_DIR}/../utils/dylib_path_manager.cc
${CMAKE_CURRENT_SOURCE_DIR}/command_line_parser.cc
Expand Down
13 changes: 2 additions & 11 deletions engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@
#include "utils/logging_utils.h"
#include "utils/scope_exit.h"
#include "utils/string_utils.h"

namespace {
// Need to change this after we rename repositories
std::string NormalizeEngine(const std::string& engine) {
if (engine == kLlamaEngine) {
return kLlamaRepo;
}
return engine;
};
} // namespace
#include "utils/normalize_engine.h"

void Engines::ListEngine(
const HttpRequestPtr& req,
Expand Down Expand Up @@ -155,7 +146,7 @@ void Engines::GetEngineVariants(
auto normalize_version = string_utils::RemoveSubstring(version, "v");
Json::Value releases(Json::arrayValue);
for (const auto& release : result.value()) {
auto json = release.ToApiJson(NormalizeEngine(engine), normalize_version);
auto json = release.ToApiJson(cortex::engine::NormalizeEngine(engine), normalize_version);
if (json != std::nullopt) {
releases.append(json.value());
}
Expand Down
47 changes: 20 additions & 27 deletions engine/services/engine_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "utils/semantic_version_utils.h"
#include "utils/system_info_utils.h"
#include "utils/url_parser.h"
#include "utils/normalize_engine.h"

namespace {
std::string GetSuitableCudaVersion(const std::string& engine,
Expand All @@ -40,14 +41,6 @@ std::string GetSuitableCudaVersion(const std::string& engine,
return suitable_toolkit_version;
}

// Need to change this after we rename repositories
std::string NormalizeEngine(const std::string& engine) {
if (engine == kLlamaEngine) {
return kLlamaRepo;
}
return engine;
};

std::string Repo2Engine(const std::string& r) {
if (r == kLlamaRepo) {
return kLlamaEngine;
Expand All @@ -66,7 +59,7 @@ std::string GetEnginePath(std::string_view e) {
cpp::result<void, std::string> EngineService::InstallEngineAsync(
const std::string& engine, const std::string& version,
const std::optional<std::string> variant_name) {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
CTL_INF("InstallEngineAsync: " << ne << ", " << version << ", "
<< variant_name.value_or(""));
auto os = hw_inf_.sys_inf->os;
Expand Down Expand Up @@ -115,7 +108,7 @@ cpp::result<bool, std::string> EngineService::UnzipEngine(
found_cuda = true;
// extract binary
auto cuda_path = file_manager_utils::GetCudaToolkitPath(
NormalizeEngine(engine), true);
cortex::engine::NormalizeEngine(engine), true);
archive_utils::ExtractArchive(path + "/" + cf, cuda_path.string(),
true);
}
Expand All @@ -141,7 +134,7 @@ cpp::result<bool, std::string> EngineService::UnzipEngine(
} else {
auto [v, ar] = engine_matcher_utils::GetVersionAndArch(matched_variant);
auto engine_path = file_manager_utils::GetEnginesContainerPath() /
NormalizeEngine(engine) / ar / v;
cortex::engine::NormalizeEngine(engine) / ar / v;
CTL_INF("engine_path: " << engine_path.string());
archive_utils::ExtractArchive(path + "/" + matched_variant,
engine_path.string(), true);
Expand All @@ -165,7 +158,7 @@ cpp::result<bool, std::string> EngineService::UnzipEngine(
cpp::result<bool, std::string> EngineService::UninstallEngineVariant(
const std::string& engine, const std::optional<std::string> version,
const std::optional<std::string> variant) {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);

// TODO: handle uninstall remote engine
// only delete a remote engine if no model are using it
Expand Down Expand Up @@ -442,15 +435,15 @@ std::string EngineService::GetMatchedVariant(

cpp::result<std::vector<EngineService::EngineRelease>, std::string>
EngineService::GetEngineReleases(const std::string& engine) const {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
return github_release_utils::GetReleases("janhq", ne);
}

cpp::result<std::vector<EngineService::EngineVariant>, std::string>
EngineService::GetEngineVariants(const std::string& engine,
const std::string& version,
bool filter_compatible_only) const {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
auto engine_release =
github_release_utils::GetReleaseByVersion("janhq", ne, version);

Expand Down Expand Up @@ -518,7 +511,7 @@ cpp::result<DefaultEngineVariant, std::string>
EngineService::SetDefaultEngineVariant(const std::string& engine,
const std::string& version,
const std::string& variant) {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
auto is_installed = IsEngineVariantReady(engine, version, variant);
if (is_installed.has_error()) {
return cpp::fail(is_installed.error());
Expand Down Expand Up @@ -560,7 +553,7 @@ EngineService::SetDefaultEngineVariant(const std::string& engine,
cpp::result<bool, std::string> EngineService::IsEngineVariantReady(
const std::string& engine, const std::string& version,
const std::string& variant) {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
auto normalized_version = string_utils::RemoveSubstring(version, "v");
auto installed_engines = GetInstalledEngineVariants(ne);
if (installed_engines.has_error()) {
Expand All @@ -583,7 +576,7 @@ cpp::result<bool, std::string> EngineService::IsEngineVariantReady(

cpp::result<DefaultEngineVariant, std::string>
EngineService::GetDefaultEngineVariant(const std::string& engine) {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
// current we don't support other engine
if (ne != kLlamaRepo) {
return cpp::fail("Engine " + engine + " is not supported yet!");
Expand All @@ -607,7 +600,7 @@ EngineService::GetDefaultEngineVariant(const std::string& engine) {

cpp::result<std::vector<EngineVariantResponse>, std::string>
EngineService::GetInstalledEngineVariants(const std::string& engine) const {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
auto os = hw_inf_.sys_inf->os;

auto engines_variants_dir =
Expand Down Expand Up @@ -650,14 +643,14 @@ EngineService::GetInstalledEngineVariants(const std::string& engine) const {
}

bool EngineService::IsEngineLoaded(const std::string& engine) {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
return engines_.find(ne) != engines_.end();
}

cpp::result<EngineV, std::string> EngineService::GetLoadedEngine(
const std::string& engine_name) {
std::lock_guard<std::mutex> lock(engines_mutex_);
auto ne = NormalizeEngine(engine_name);
auto ne = cortex::engine::NormalizeEngine(engine_name);
if (engines_.find(ne) == engines_.end()) {
return cpp::fail("Engine " + engine_name + " is not loaded yet!");
}
Expand All @@ -667,7 +660,7 @@ cpp::result<EngineV, std::string> EngineService::GetLoadedEngine(

cpp::result<void, std::string> EngineService::LoadEngine(
const std::string& engine_name) {
auto ne = NormalizeEngine(engine_name);
auto ne = cortex::engine::NormalizeEngine(engine_name);
std::lock_guard<std::mutex> lock(engines_mutex_);
if (IsEngineLoaded(ne)) {
CTL_INF("Engine " << ne << " is already loaded");
Expand Down Expand Up @@ -771,7 +764,7 @@ cpp::result<void, std::string> EngineService::LoadEngine(
void EngineService::RegisterEngineLibPath() {
auto engine_names = GetSupportedEngineNames().value();
for (const auto& engine : engine_names) {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
try {
auto engine_dir_path_res = GetEngineDirPath(engine);
if (engine_dir_path_res.has_error()) {
Expand Down Expand Up @@ -809,7 +802,7 @@ void EngineService::RegisterEngineLibPath() {

cpp::result<std::pair<std::filesystem::path, bool>, std::string>
EngineService::GetEngineDirPath(const std::string& engine_name) {
auto ne = NormalizeEngine(engine_name);
auto ne = cortex::engine::NormalizeEngine(engine_name);

auto selected_engine_variant = GetDefaultEngineVariant(ne);

Expand Down Expand Up @@ -852,7 +845,7 @@ EngineService::GetEngineDirPath(const std::string& engine_name) {

cpp::result<void, std::string> EngineService::UnloadEngine(
const std::string& engine) {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);

std::lock_guard<std::mutex> lock(engines_mutex_);
if (!IsEngineLoaded(ne)) {
Expand Down Expand Up @@ -890,7 +883,7 @@ std::vector<EngineV> EngineService::GetLoadedEngines() {

cpp::result<github_release_utils::GitHubRelease, std::string>
EngineService::GetLatestEngineVersion(const std::string& engine) const {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
auto res = github_release_utils::GetReleaseByVersion("janhq", ne, "latest");
if (res.has_error()) {
return cpp::fail("Failed to fetch engine " + engine + " latest version!");
Expand All @@ -900,7 +893,7 @@ EngineService::GetLatestEngineVersion(const std::string& engine) const {

cpp::result<bool, std::string> EngineService::IsEngineReady(
const std::string& engine) {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);

// Check for remote engine
if (IsRemoteEngine(engine)) {
Expand Down Expand Up @@ -929,7 +922,7 @@ cpp::result<bool, std::string> EngineService::IsEngineReady(

cpp::result<EngineUpdateResult, std::string> EngineService::UpdateEngine(
const std::string& engine) {
auto ne = NormalizeEngine(engine);
auto ne = cortex::engine::NormalizeEngine(engine);
auto default_variant = GetDefaultEngineVariant(ne);

if (default_variant.has_error()) {
Expand Down
13 changes: 13 additions & 0 deletions engine/utils/normalize_engine.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "normalize_engine.h"
#include "engine_constants.h"

namespace cortex::engine {

std::string NormalizeEngine(const std::string& engine) {
if (engine == kLlamaEngine) {
return kLlamaRepo;
}
return engine;
}

} // namespace cortex::engine
8 changes: 8 additions & 0 deletions engine/utils/normalize_engine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <string>

namespace cortex::engine {
// Declaration of the NormalizeEngine function
std::string NormalizeEngine(const std::string& engine);
} // namespace cortex::engine
Loading