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

Commit 082e5f4

Browse files
committed
store default engine to cortex.rc
1 parent 4b30b22 commit 082e5f4

File tree

6 files changed

+89
-22
lines changed

6 files changed

+89
-22
lines changed

engine/cli/command_line_parser.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,11 @@ void CommandLineParser::SetupSystemCommands() {
393393
<< " to " << cml_data_.port);
394394
auto config_path = file_manager_utils::GetConfigurationPath();
395395
cml_data_.config.apiServerPort = std::to_string(cml_data_.port);
396-
config_yaml_utils::DumpYamlConfig(cml_data_.config, config_path.string());
396+
auto result = config_yaml_utils::DumpYamlConfig(cml_data_.config,
397+
config_path.string());
398+
if (result.has_error()) {
399+
CLI_LOG("Error update " << config_path.string() << result.error());
400+
}
397401
}
398402
commands::ServerStartCmd ssc;
399403
ssc.Exec(cml_data_.config.apiServerHost,

engine/main.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ void RunServer(std::optional<int> port) {
4747
if (port.has_value() && *port != std::stoi(config.apiServerPort)) {
4848
auto config_path = file_manager_utils::GetConfigurationPath();
4949
config.apiServerPort = std::to_string(*port);
50-
config_yaml_utils::DumpYamlConfig(config, config_path.string());
50+
auto result =
51+
config_yaml_utils::DumpYamlConfig(config, config_path.string());
52+
if (result.has_error()) {
53+
CTL_ERR("Error update " << config_path.string() << result.error());
54+
}
5155
}
5256
std::cout << "Host: " << config.apiServerHost
5357
<< " Port: " << config.apiServerPort << "\n";
@@ -147,7 +151,12 @@ int main(int argc, char* argv[]) {
147151
}
148152
}
149153

150-
{ file_manager_utils::CreateConfigFileIfNotExist(); }
154+
{
155+
auto result = file_manager_utils::CreateConfigFileIfNotExist();
156+
if (result.has_error()) {
157+
LOG_ERROR << "Error creating config file: " << result.error();
158+
}
159+
}
151160

152161
// Delete temporary file if it exists
153162
auto temp =

engine/services/engine_service.cc

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -527,12 +527,19 @@ EngineService::SetDefaultEngineVariant(const std::string& engine,
527527
" is not installed yet!");
528528
}
529529

530-
default_variants_[ne] = DefaultEngineVariant{
530+
auto config = file_manager_utils::GetCortexConfig();
531+
config.llamacppVersion = version;
532+
config.llamacppVariant = variant;
533+
auto result = file_manager_utils::UpdateCortexConfig(config);
534+
if (result.has_error()) {
535+
return cpp::fail(result.error());
536+
}
537+
538+
return DefaultEngineVariant{
531539
.engine = engine,
532540
.version = version,
533541
.variant = variant,
534542
};
535-
return default_variants_[ne];
536543
}
537544

538545
cpp::result<bool, std::string> EngineService::IsEngineVariantReady(
@@ -554,11 +561,25 @@ cpp::result<bool, std::string> EngineService::IsEngineVariantReady(
554561
cpp::result<DefaultEngineVariant, std::string>
555562
EngineService::GetDefaultEngineVariant(const std::string& engine) {
556563
auto ne = NormalizeEngine(engine);
557-
if (default_variants_.find(ne) == default_variants_.end()) {
558-
return cpp::fail("Engine variant for " + engine + " is not set yet!");
564+
// current we don't support other engine
565+
if (ne != kLlamaRepo) {
566+
return cpp::fail("Engine " + engine + " is not supported yet!");
567+
}
568+
569+
auto config = file_manager_utils::GetCortexConfig();
570+
auto variant = config.llamacppVariant;
571+
auto version = config.llamacppVersion;
572+
573+
if (variant.empty() || version.empty()) {
574+
return cpp::fail("Default engine variant for " + engine +
575+
" is not set yet!");
559576
}
560577

561-
return default_variants_[ne];
578+
return DefaultEngineVariant{
579+
.engine = engine,
580+
.version = version,
581+
.variant = variant,
582+
};
562583
}
563584

564585
std::vector<EngineVariantResponse> EngineService::GetInstalledEngineVariants(

engine/services/engine_service.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,4 @@ class EngineService {
183183
std::string cuda_driver_version;
184184
};
185185
HardwareInfo hw_inf_;
186-
187-
std::unordered_map<std::string, DefaultEngineVariant> default_variants_;
188186
};

engine/utils/config_yaml_utils.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <iostream>
55
#include <string>
66
#include "utils/logging_utils.h"
7+
#include "utils/result.hpp"
78
#include "yaml-cpp/yaml.h"
89

910
namespace config_yaml_utils {
@@ -21,11 +22,12 @@ struct CortexConfig {
2122
std::string latestRelease;
2223

2324
std::string huggingFaceToken;
24-
2525
/**
2626
* Github's API requires a user-agent string.
2727
*/
2828
std::string gitHubUserAgent;
29+
std::string llamacppVariant;
30+
std::string llamacppVersion;
2931
};
3032

3133
const std::string kDefaultHost{"127.0.0.1"};
@@ -34,8 +36,8 @@ const int kDefaultMaxLines{100000};
3436
constexpr const uint64_t kDefaultCheckedForUpdateAt = 0u;
3537
constexpr const auto kDefaultLatestRelease = "default_version";
3638

37-
inline void DumpYamlConfig(const CortexConfig& config,
38-
const std::string& path) {
39+
inline cpp::result<void, std::string> DumpYamlConfig(const CortexConfig& config,
40+
const std::string& path) {
3941
std::filesystem::path config_file_path{path};
4042

4143
try {
@@ -56,12 +58,15 @@ inline void DumpYamlConfig(const CortexConfig& config,
5658
node["latestRelease"] = config.latestRelease;
5759
node["huggingFaceToken"] = config.huggingFaceToken;
5860
node["gitHubUserAgent"] = config.gitHubUserAgent;
61+
node["llamacppVariant"] = config.llamacppVariant;
62+
node["llamacppVersion"] = config.llamacppVersion;
5963

6064
out_file << node;
6165
out_file.close();
66+
return {};
6267
} catch (const std::exception& e) {
6368
CTL_ERR("Error writing to file: " << e.what());
64-
throw;
69+
return cpp::fail("Error writing to file: " + std::string(e.what()));
6570
}
6671
}
6772

@@ -80,7 +85,8 @@ inline CortexConfig FromYaml(const std::string& path,
8085
!node["apiServerPort"] || !node["checkedForUpdateAt"] ||
8186
!node["latestRelease"] || !node["logLlamaCppPath"] ||
8287
!node["logOnnxPath"] || !node["logTensorrtLLMPath"] ||
83-
!node["huggingFaceToken"] || !node["gitHubUserAgent"]);
88+
!node["huggingFaceToken"] || !node["gitHubUserAgent"] ||
89+
!node["llamacppVariant"] || !node["llamacppVersion"]);
8490

8591
CortexConfig config = {
8692
.logFolderPath = node["logFolderPath"]
@@ -118,9 +124,18 @@ inline CortexConfig FromYaml(const std::string& path,
118124
.gitHubUserAgent = node["gitHubUserAgent"]
119125
? node["gitHubUserAgent"].as<std::string>()
120126
: "",
127+
.llamacppVariant = node["llamacppVariant"]
128+
? node["llamacppVariant"].as<std::string>()
129+
: "",
130+
.llamacppVersion = node["llamacppVersion"]
131+
? node["llamacppVersion"].as<std::string>()
132+
: "",
121133
};
122134
if (should_update_config) {
123-
DumpYamlConfig(config, path);
135+
auto result = DumpYamlConfig(config, path);
136+
if (result.has_error()) {
137+
CTL_ERR("Failed to update config file: " << result.error());
138+
}
124139
}
125140
return config;
126141
} catch (const YAML::BadFile& e) {

engine/utils/file_manager_utils.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "logging_utils.h"
77
#include "utils/config_yaml_utils.h"
88
#include "utils/engine_constants.h"
9+
#include "utils/result.hpp"
910

1011
#if defined(__APPLE__) && defined(__MACH__)
1112
#include <mach-o/dyld.h>
@@ -138,11 +139,22 @@ inline std::string GetDefaultDataFolderName() {
138139
return default_data_folder_name;
139140
}
140141

141-
inline void CreateConfigFileIfNotExist() {
142+
inline cpp::result<void, std::string> UpdateCortexConfig(
143+
const config_yaml_utils::CortexConfig& config) {
144+
auto config_path = GetConfigurationPath();
145+
if (!std::filesystem::exists(config_path)) {
146+
CTL_ERR("Config file not found: " << config_path.string());
147+
return cpp::fail("Config file not found: " + config_path.string());
148+
}
149+
150+
return DumpYamlConfig(config, config_path.string());
151+
}
152+
153+
inline cpp::result<void, std::string> CreateConfigFileIfNotExist() {
142154
auto config_path = GetConfigurationPath();
143155
if (std::filesystem::exists(config_path)) {
144-
// already exists
145-
return;
156+
// already exists, no need to create
157+
return {};
146158
}
147159

148160
auto default_data_folder_name = GetDefaultDataFolderName();
@@ -165,7 +177,7 @@ inline void CreateConfigFileIfNotExist() {
165177
.apiServerHost = config_yaml_utils::kDefaultHost,
166178
.apiServerPort = config_yaml_utils::kDefaultPort,
167179
};
168-
DumpYamlConfig(config, config_path.string());
180+
return DumpYamlConfig(config, config_path.string());
169181
}
170182

171183
inline config_yaml_utils::CortexConfig GetCortexConfig() {
@@ -190,7 +202,12 @@ inline config_yaml_utils::CortexConfig GetCortexConfig() {
190202
}
191203

192204
inline std::filesystem::path GetCortexDataPath() {
193-
CreateConfigFileIfNotExist();
205+
auto result = CreateConfigFileIfNotExist();
206+
if (result.has_error()) {
207+
CTL_ERR("Error creating config file: " << result.error());
208+
return std::filesystem::path{};
209+
}
210+
194211
auto config = GetCortexConfig();
195212
std::filesystem::path data_folder_path;
196213
if (!config.dataFolderPath.empty()) {
@@ -239,7 +256,10 @@ inline void CreateDirectoryRecursively(const std::string& path) {
239256
}
240257

241258
inline std::filesystem::path GetModelsContainerPath() {
242-
CreateConfigFileIfNotExist();
259+
auto result = CreateConfigFileIfNotExist();
260+
if (result.has_error()) {
261+
CTL_ERR("Error creating config file: " << result.error());
262+
}
243263
auto cortex_path = GetCortexDataPath();
244264
auto models_container_path = cortex_path / "models";
245265

0 commit comments

Comments
 (0)