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

Commit 14f991a

Browse files
committed
Merge branch 'fix/db-rel-path' of github.com:janhq/nitro into fix/db-rel-path
2 parents 50b38b5 + 167c807 commit 14f991a

File tree

6 files changed

+52
-21
lines changed

6 files changed

+52
-21
lines changed

engine/commands/model_import_cmd.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ ModelImportCmd::ModelImportCmd(std::string model_handle, std::string model_path)
1414
model_path_(std::move(model_path)) {}
1515

1616
void ModelImportCmd::Exec() {
17+
namespace fs = std::filesystem;
18+
namespace fmu = file_manager_utils;
1719
config::GGUFHandler gguf_handler;
1820
config::YamlHandler yaml_handler;
1921
cortex::db::Models modellist_utils_obj;
@@ -22,10 +24,13 @@ void ModelImportCmd::Exec() {
2224
std::filesystem::path("imported") /
2325
std::filesystem::path(model_handle_ + ".yml"))
2426
.string();
25-
cortex::db::ModelEntry model_entry{
26-
model_handle_, "local", "imported",
27-
model_yaml_path, model_handle_};
2827
try {
28+
// Use relative path for model_yaml_path. In case of import, we use absolute path for model
29+
auto yaml_rel_path =
30+
fmu::Subtract(fs::path(model_yaml_path), fmu::GetCortexDataPath());
31+
cortex::db::ModelEntry model_entry{model_handle_, "local", "imported",
32+
yaml_rel_path, model_handle_};
33+
2934
std::filesystem::create_directories(
3035
std::filesystem::path(model_yaml_path).parent_path());
3136
gguf_handler.Parse(model_path_);

engine/config/yaml_config.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using namespace std;
77

88
#include "utils/format_utils.h"
9+
#include "utils/file_manager_utils.h"
910
#include "yaml_config.h"
1011
namespace config {
1112
// Method to read YAML file
@@ -14,6 +15,8 @@ void YamlHandler::Reset() {
1415
yaml_node_.reset();
1516
};
1617
void YamlHandler::ReadYamlFile(const std::string& file_path) {
18+
namespace fs = std::filesystem;
19+
namespace fmu = file_manager_utils;
1720
try {
1821
yaml_node_ = YAML::LoadFile(file_path);
1922
// incase of model.yml file, we don't have files yet, create them
@@ -24,8 +27,9 @@ void YamlHandler::ReadYamlFile(const std::string& file_path) {
2427
std::vector<std::string> v;
2528
if (yaml_node_["engine"] &&
2629
yaml_node_["engine"].as<std::string>() == "cortex.llamacpp") {
27-
// TODO: change prefix to models:// with source from cortexso
28-
v.emplace_back(s.substr(0, s.find_last_of('/')) + "/model.gguf");
30+
auto abs_path = s.substr(0, s.find_last_of('/')) + "/model.gguf";
31+
auto rel_path = fmu::Subtract(fs::path(abs_path), fmu::GetCortexDataPath());
32+
v.emplace_back(rel_path.string());
2933
} else {
3034
v.emplace_back(s.substr(0, s.find_last_of('/')));
3135
}

engine/controllers/models.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ void Models::UpdateModel(const HttpRequestPtr& req,
224224
void Models::ImportModel(
225225
const HttpRequestPtr& req,
226226
std::function<void(const HttpResponsePtr&)>&& callback) const {
227+
namespace fs = std::filesystem;
228+
namespace fmu = file_manager_utils;
227229
if (!http_util::HasFieldInReq(req, callback, "model") ||
228230
!http_util::HasFieldInReq(req, callback, "modelPath")) {
229231
return;
@@ -233,14 +235,18 @@ void Models::ImportModel(
233235
config::GGUFHandler gguf_handler;
234236
config::YamlHandler yaml_handler;
235237
cortex::db::Models modellist_utils_obj;
236-
237238
std::string model_yaml_path = (file_manager_utils::GetModelsContainerPath() /
238239
std::filesystem::path("imported") /
239240
std::filesystem::path(modelHandle + ".yml"))
240241
.string();
241-
cortex::db::ModelEntry model_entry{modelHandle, "local", "imported",
242-
model_yaml_path, modelHandle};
242+
243243
try {
244+
// Use relative path for model_yaml_path. In case of import, we use absolute path for model
245+
auto yaml_rel_path =
246+
fmu::Subtract(fs::path(model_yaml_path), fmu::GetCortexDataPath());
247+
cortex::db::ModelEntry model_entry{modelHandle, "local", "imported",
248+
yaml_rel_path, modelHandle};
249+
244250
std::filesystem::create_directories(
245251
std::filesystem::path(model_yaml_path).parent_path());
246252
gguf_handler.Parse(modelPath);

engine/services/model_service.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
namespace {
1818
void ParseGguf(const DownloadItem& ggufDownloadItem,
1919
std::optional<std::string> author) {
20-
20+
namespace fs = std::filesystem;
21+
namespace fmu = file_manager_utils;
2122
config::GGUFHandler gguf_handler;
2223
config::YamlHandler yaml_handler;
2324
gguf_handler.Parse(ggufDownloadItem.localPath.string());
25+
2426
config::ModelConfig model_config = gguf_handler.GetModelConfig();
2527
model_config.id =
2628
ggufDownloadItem.localPath.parent_path().filename().string();
27-
model_config.files = {ggufDownloadItem.localPath.string()};
29+
// use relative path for files
30+
auto file_rel_path = fmu::Subtract(fs::path(ggufDownloadItem.localPath),
31+
fmu::GetCortexDataPath());
32+
model_config.files = {file_rel_path.string()};
2833
model_config.model = ggufDownloadItem.id;
2934
yaml_handler.UpdateModelConfig(model_config);
3035

@@ -383,11 +388,13 @@ cpp::result<void, std::string> ModelService::DeleteModel(
383388
if (mc.files.size() > 0) {
384389
if (mc.engine == "cortex.llamacpp") {
385390
for (auto& file : mc.files) {
386-
std::filesystem::path gguf_p(file);
391+
std::filesystem::path gguf_p(
392+
fmu::GetAbsolutePath(fmu::GetCortexDataPath(), fs::path(file)));
387393
std::filesystem::remove(gguf_p);
388394
}
389395
} else {
390-
std::filesystem::path f(mc.files[0]);
396+
std::filesystem::path f(fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
397+
fs::path(mc.files[0])));
391398
std::filesystem::remove_all(f);
392399
}
393400
} else {
@@ -431,7 +438,9 @@ cpp::result<bool, std::string> ModelService::StartModel(
431438
Json::Value json_data = mc.ToJson();
432439
if (mc.files.size() > 0) {
433440
// TODO(sang) support multiple files
434-
json_data["model_path"] = mc.files[0];
441+
json_data["model_path"] =
442+
fmu::GetAbsolutePath(fmu::GetCortexDataPath(), fs::path(mc.files[0]))
443+
.string();
435444
} else {
436445
LOG_WARN << "model_path is empty";
437446
return false;

engine/test/components/test_paths.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,32 @@ TEST_F(PathTests, IsSubpath_Subpath_ReturnsTrue) {
2727

2828
TEST_F(PathTests, IsSubpath_NotSubpath_ReturnsFalse) {
2929
auto base = std::filesystem::path("/base");
30-
auto notSubpath = std::filesystem::path("/other/path");
31-
EXPECT_FALSE(IsSubpath(base, notSubpath));
30+
auto not_subpath = std::filesystem::path("/other/path");
31+
EXPECT_FALSE(IsSubpath(base, not_subpath));
3232
}
3333

3434
TEST_F(PathTests, IsSubpath_EmptyRelative_ReturnsFalse) {
3535
auto base = std::filesystem::path("/base");
36-
auto emptyPath = std::filesystem::path("/base/");
37-
EXPECT_FALSE(IsSubpath(base, emptyPath));
36+
auto empty_path = std::filesystem::path("");
37+
EXPECT_FALSE(IsSubpath(base, empty_path));
38+
}
39+
40+
TEST_F(PathTests, IsSubpath_SamePath_ReturnsTrue) {
41+
auto base = std::filesystem::path("/base");
42+
EXPECT_TRUE(IsSubpath(base, base));
3843
}
3944

4045
// Test cases for Subtract
4146
TEST_F(PathTests, Subtract_SubtractingBaseFromSubPath_ReturnsRelativePath) {
4247
auto base = std::filesystem::path("/base");
43-
auto subPath = std::filesystem::path("/base/relative/path");
44-
EXPECT_EQ(Subtract(subPath, base), std::filesystem::path("relative/path"));
48+
auto subpath = std::filesystem::path("/base/relative/path");
49+
EXPECT_EQ(Subtract(subpath, base), std::filesystem::path("relative/path"));
4550
}
4651

4752
TEST_F(PathTests, Subtract_NotASubPath_ReturnsOriginalPath) {
4853
auto base = std::filesystem::path("/base");
49-
auto notSubPath = std::filesystem::path("/other/path");
50-
EXPECT_EQ(Subtract(notSubPath, base), notSubPath);
54+
auto not_subpath = std::filesystem::path("/other/path");
55+
EXPECT_EQ(Subtract(not_subpath, base), not_subpath);
5156
}
5257

5358
TEST_F(PathTests, Subtract_IdenticalPaths_ReturnsEmptyRelative) {

engine/utils/file_manager_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ inline std::filesystem::path GetAbsolutePath(const std::filesystem::path& base,
302302

303303
inline bool IsSubpath(const std::filesystem::path& base,
304304
const std::filesystem::path& path) {
305+
if (base == path)
306+
return true;
305307
auto rel = std::filesystem::relative(path, base);
306308
return !rel.empty() && rel.native()[0] != '.';
307309
}

0 commit comments

Comments
 (0)