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

Commit 18e5634

Browse files
chore: move logic to file manager utils (#1405)
1 parent dec2a6c commit 18e5634

File tree

10 files changed

+47
-42
lines changed

10 files changed

+47
-42
lines changed

engine/commands/chat_completion_cmd.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ void ChatCompletionCmd::Exec(const std::string& host, int port,
5252
return;
5353
}
5454
yaml_handler.ModelConfigFromFile(
55-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
56-
fs::path(model_entry.value().path_to_model_yaml))
55+
fmu::ToAbsoluteCortexDataPath(
56+
fs::path(model_entry.value().path_to_model_yaml))
5757
.string());
5858
auto mc = yaml_handler.GetModelConfig();
5959
Exec(host, port, model_handle, mc, std::move(msg));

engine/commands/model_get_cmd.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ void ModelGetCmd::Exec(const std::string& model_handle) {
2222
return;
2323
}
2424
yaml_handler.ModelConfigFromFile(
25-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
26-
fs::path(model_entry.value().path_to_model_yaml))
25+
fmu::ToAbsoluteCortexDataPath(
26+
fs::path(model_entry.value().path_to_model_yaml))
2727
.string());
2828
auto model_config = yaml_handler.GetModelConfig();
2929

engine/commands/model_import_cmd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void ModelImportCmd::Exec() {
2727
try {
2828
// Use relative path for model_yaml_path. In case of import, we use absolute path for model
2929
auto yaml_rel_path =
30-
fmu::Subtract(fs::path(model_yaml_path), fmu::GetCortexDataPath());
30+
fmu::ToRelativeCortexDataPath(fs::path(model_yaml_path));
3131
cortex::db::ModelEntry model_entry{model_handle_, "local", "imported",
3232
yaml_rel_path.string(), model_handle_};
3333

engine/commands/model_list_cmd.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ void ModelListCmd::Exec() {
3232
try {
3333
count += 1;
3434
yaml_handler.ModelConfigFromFile(
35-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
36-
fs::path(model_entry.path_to_model_yaml))
37-
.string());
35+
fmu::ToAbsoluteCortexDataPath(
36+
fs::path(model_entry.path_to_model_yaml))
37+
.string());
3838
auto model_config = yaml_handler.GetModelConfig();
3939
table.add_row({std::to_string(count), model_entry.model,
4040
model_entry.model_alias, model_config.engine,

engine/commands/model_upd_cmd.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ void ModelUpdCmd::Exec(
1717
CLI_LOG("Error: " + model_entry.error());
1818
return;
1919
}
20-
auto yaml_fp =
21-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
22-
fs::path(model_entry.value().path_to_model_yaml));
20+
auto yaml_fp = fmu::ToAbsoluteCortexDataPath(
21+
fs::path(model_entry.value().path_to_model_yaml));
2322
yaml_handler_.ModelConfigFromFile(yaml_fp.string());
2423
model_config_ = yaml_handler_.GetModelConfig();
2524

engine/commands/run_cmd.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ void RunCmd::Exec(bool chat_flag) {
4040
return;
4141
}
4242
yaml_handler.ModelConfigFromFile(
43-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
44-
fs::path(model_entry.value().path_to_model_yaml))
43+
fmu::ToAbsoluteCortexDataPath(
44+
fs::path(model_entry.value().path_to_model_yaml))
4545
.string());
4646
auto mc = yaml_handler.GetModelConfig();
4747

engine/config/yaml_config.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void YamlHandler::ReadYamlFile(const std::string& file_path) {
2727
if (yaml_node_["engine"] &&
2828
yaml_node_["engine"].as<std::string>() == "cortex.llamacpp") {
2929
auto abs_path = s.substr(0, s.find_last_of('/')) + "/model.gguf";
30-
auto rel_path = fmu::Subtract(fs::path(abs_path), fmu::GetCortexDataPath());
30+
auto rel_path = fmu::ToRelativeCortexDataPath(fs::path(abs_path));
3131
v.emplace_back(rel_path.string());
3232
} else {
3333
v.emplace_back(s.substr(0, s.find_last_of('/')));

engine/controllers/models.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ void Models::ListModel(
7676
// auto model_entry = modellist_handler.GetModelInfo(model_handle);
7777
try {
7878
yaml_handler.ModelConfigFromFile(
79-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
80-
fs::path(model_entry.path_to_model_yaml))
79+
fmu::ToAbsoluteCortexDataPath(
80+
fs::path(model_entry.path_to_model_yaml))
8181
.string());
8282
auto model_config = yaml_handler.GetModelConfig();
8383
Json::Value obj = model_config.ToJson();
@@ -132,8 +132,8 @@ void Models::GetModel(const HttpRequestPtr& req,
132132
return;
133133
}
134134
yaml_handler.ModelConfigFromFile(
135-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
136-
fs::path(model_entry.value().path_to_model_yaml))
135+
fmu::ToAbsoluteCortexDataPath(
136+
fs::path(model_entry.value().path_to_model_yaml))
137137
.string());
138138
auto model_config = yaml_handler.GetModelConfig();
139139

@@ -187,9 +187,8 @@ void Models::UpdateModel(const HttpRequestPtr& req,
187187
cortex::db::Models model_list_utils;
188188
auto model_entry = model_list_utils.GetModelInfo(model_id);
189189
config::YamlHandler yaml_handler;
190-
auto yaml_fp =
191-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
192-
fs::path(model_entry.value().path_to_model_yaml));
190+
auto yaml_fp = fmu::ToAbsoluteCortexDataPath(
191+
fs::path(model_entry.value().path_to_model_yaml));
193192
yaml_handler.ModelConfigFromFile(yaml_fp.string());
194193
config::ModelConfig model_config = yaml_handler.GetModelConfig();
195194
model_config.FromJson(json_body);
@@ -243,7 +242,7 @@ void Models::ImportModel(
243242
try {
244243
// Use relative path for model_yaml_path. In case of import, we use absolute path for model
245244
auto yaml_rel_path =
246-
fmu::Subtract(fs::path(model_yaml_path), fmu::GetCortexDataPath());
245+
fmu::ToRelativeCortexDataPath(fs::path(model_yaml_path));
247246
cortex::db::ModelEntry model_entry{modelHandle, "local", "imported",
248247
yaml_rel_path.string(), modelHandle};
249248

engine/services/model_service.cc

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ void ParseGguf(const DownloadItem& ggufDownloadItem,
2727
model_config.id =
2828
ggufDownloadItem.localPath.parent_path().filename().string();
2929
// use relative path for files
30-
auto file_rel_path = fmu::Subtract(fs::path(ggufDownloadItem.localPath),
31-
fmu::GetCortexDataPath());
30+
auto file_rel_path =
31+
fmu::ToRelativeCortexDataPath(fs::path(ggufDownloadItem.localPath));
3232
model_config.files = {file_rel_path.string()};
3333
model_config.model = ggufDownloadItem.id;
3434
yaml_handler.UpdateModelConfig(model_config);
@@ -44,8 +44,7 @@ void ParseGguf(const DownloadItem& ggufDownloadItem,
4444
auto branch = url_obj.pathParams[3];
4545
CTL_INF("Adding model to modellist with branch: " << branch);
4646

47-
auto rel = file_manager_utils::Subtract(
48-
yaml_name, file_manager_utils::GetCortexDataPath());
47+
auto rel = file_manager_utils::ToRelativeCortexDataPath(yaml_name);
4948
CTL_INF("path_to_model_yaml: " << rel.string());
5049

5150
auto author_id = author.has_value() ? author.value() : "cortexso";
@@ -304,8 +303,8 @@ cpp::result<std::string, std::string> ModelService::DownloadModelFromCortexso(
304303
yaml_handler.UpdateModelConfig(mc);
305304
yaml_handler.WriteYamlFile(model_yml_item->localPath.string());
306305

307-
auto rel = file_manager_utils::Subtract(
308-
model_yml_item->localPath, file_manager_utils::GetCortexDataPath());
306+
auto rel =
307+
file_manager_utils::ToRelativeCortexDataPath(model_yml_item->localPath);
309308
CTL_INF("path_to_model_yaml: " << rel.string());
310309

311310
cortex::db::Models modellist_utils_obj;
@@ -376,9 +375,8 @@ cpp::result<void, std::string> ModelService::DeleteModel(
376375
CTL_WRN("Error: " + model_entry.error());
377376
return cpp::fail(model_entry.error());
378377
}
379-
auto yaml_fp =
380-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
381-
fs::path(model_entry.value().path_to_model_yaml));
378+
auto yaml_fp = fmu::ToAbsoluteCortexDataPath(
379+
fs::path(model_entry.value().path_to_model_yaml));
382380
yaml_handler.ModelConfigFromFile(yaml_fp.string());
383381
auto mc = yaml_handler.GetModelConfig();
384382
// Remove yaml file
@@ -389,12 +387,12 @@ cpp::result<void, std::string> ModelService::DeleteModel(
389387
if (mc.engine == "cortex.llamacpp") {
390388
for (auto& file : mc.files) {
391389
std::filesystem::path gguf_p(
392-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(), fs::path(file)));
390+
fmu::ToAbsoluteCortexDataPath(fs::path(file)));
393391
std::filesystem::remove(gguf_p);
394392
}
395393
} else {
396-
std::filesystem::path f(fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
397-
fs::path(mc.files[0])));
394+
std::filesystem::path f(
395+
fmu::ToAbsoluteCortexDataPath(fs::path(mc.files[0])));
398396
std::filesystem::remove_all(f);
399397
}
400398
} else {
@@ -428,8 +426,8 @@ cpp::result<bool, std::string> ModelService::StartModel(
428426
return cpp::fail(model_entry.error());
429427
}
430428
yaml_handler.ModelConfigFromFile(
431-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
432-
fs::path(model_entry.value().path_to_model_yaml))
429+
fmu::ToAbsoluteCortexDataPath(
430+
fs::path(model_entry.value().path_to_model_yaml))
433431
.string());
434432
auto mc = yaml_handler.GetModelConfig();
435433

@@ -439,8 +437,7 @@ cpp::result<bool, std::string> ModelService::StartModel(
439437
if (mc.files.size() > 0) {
440438
// TODO(sang) support multiple files
441439
json_data["model_path"] =
442-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(), fs::path(mc.files[0]))
443-
.string();
440+
fmu::ToAbsoluteCortexDataPath(fs::path(mc.files[0])).string();
444441
} else {
445442
LOG_WARN << "model_path is empty";
446443
return false;
@@ -489,8 +486,8 @@ cpp::result<bool, std::string> ModelService::StopModel(
489486
return cpp::fail(model_entry.error());
490487
}
491488
yaml_handler.ModelConfigFromFile(
492-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
493-
fs::path(model_entry.value().path_to_model_yaml))
489+
fmu::ToAbsoluteCortexDataPath(
490+
fs::path(model_entry.value().path_to_model_yaml))
494491
.string());
495492
auto mc = yaml_handler.GetModelConfig();
496493

@@ -538,8 +535,8 @@ cpp::result<bool, std::string> ModelService::GetModelStatus(
538535
return cpp::fail(model_entry.error());
539536
}
540537
yaml_handler.ModelConfigFromFile(
541-
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
542-
fs::path(model_entry.value().path_to_model_yaml))
538+
fmu::ToAbsoluteCortexDataPath(
539+
fs::path(model_entry.value().path_to_model_yaml))
543540
.string());
544541
auto mc = yaml_handler.GetModelConfig();
545542

engine/utils/file_manager_utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,14 @@ inline std::filesystem::path Subtract(const std::filesystem::path& path,
317317
}
318318
}
319319

320+
inline std::filesystem::path ToRelativeCortexDataPath(
321+
const std::filesystem::path& path) {
322+
return Subtract(path, GetCortexDataPath());
323+
}
324+
325+
inline std::filesystem::path ToAbsoluteCortexDataPath(
326+
const std::filesystem::path& path) {
327+
return GetAbsolutePath(GetCortexDataPath(), path);
328+
}
329+
320330
} // namespace file_manager_utils

0 commit comments

Comments
 (0)