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

Commit 23a4b7d

Browse files
authored
Merge pull request #2009 from janhq/s/chore/sync-dev
chore: sync dev to main
2 parents c9e8197 + f2de6e1 commit 23a4b7d

File tree

10 files changed

+162
-36
lines changed

10 files changed

+162
-36
lines changed

engine/cli/commands/ps_cmd.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ void PsCmd::Exec(const std::string& host, int port) {
2727
for (const auto& item : res.value()["data"]) {
2828
ModelLoadedStatus model_status;
2929
// TODO(sang) hardcode for now
30-
model_status.engine = kLlamaEngine;
30+
model_status.engine = item["engine"].isNull()
31+
? kLlamaEngine : item["engine"].asString();
3132
model_status.model = item["id"].asString();
3233
model_status.ram = item["ram"].asUInt64();
3334
model_status.start_time = item["start_time"].asUInt64();

engine/controllers/models.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,4 +854,31 @@ void Models::GetModelSource(
854854
resp->setStatusCode(k200OK);
855855
callback(resp);
856856
}
857+
}
858+
859+
void Models::GetRepositoryList(
860+
const HttpRequestPtr& req,
861+
std::function<void(const HttpResponsePtr&)>&& callback,
862+
std::optional<std::string> author) {
863+
if (!author.has_value())
864+
author = "cortexso";
865+
auto res = model_src_svc_->GetRepositoryList(author.value());
866+
if (res.has_error()) {
867+
Json::Value ret;
868+
ret["message"] = res.error();
869+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
870+
resp->setStatusCode(k400BadRequest);
871+
callback(resp);
872+
} else {
873+
auto& info = res.value();
874+
Json::Value ret;
875+
Json::Value arr(Json::arrayValue);
876+
for (auto const& i : info) {
877+
arr.append(i);
878+
}
879+
ret["data"] = arr;
880+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
881+
resp->setStatusCode(k200OK);
882+
callback(resp);
883+
}
857884
}

engine/controllers/models.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Models : public drogon::HttpController<Models, false> {
4444
ADD_METHOD_TO(Models::DeleteModelSource, "/v1/models/sources", Delete);
4545
ADD_METHOD_TO(Models::GetModelSources, "/v1/models/sources", Get);
4646
ADD_METHOD_TO(Models::GetModelSource, "/v1/models/sources/{src}", Get);
47+
ADD_METHOD_TO(Models::GetRepositoryList, "/v1/models/hub?author={author}",
48+
Get);
4749
METHOD_LIST_END
4850

4951
explicit Models(std::shared_ptr<DatabaseService> db_service,
@@ -111,6 +113,10 @@ class Models : public drogon::HttpController<Models, false> {
111113
std::function<void(const HttpResponsePtr&)>&& callback,
112114
const std::string& src);
113115

116+
void GetRepositoryList(const HttpRequestPtr& req,
117+
std::function<void(const HttpResponsePtr&)>&& callback,
118+
std::optional<std::string> author);
119+
114120
private:
115121
std::shared_ptr<DatabaseService> db_service_;
116122
std::shared_ptr<ModelService> model_service_;

engine/repositories/file_fs_repository.cc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ std::filesystem::path FileFsRepository::GetFilePath() const {
1010
return data_folder_path_ / kFileContainerFolderName;
1111
}
1212

13+
std::filesystem::path SanitizePath(const std::filesystem::path & user_input,
14+
const std::filesystem::path & basedir) {
15+
16+
auto abs_base = std::filesystem::canonical(basedir);
17+
std::filesystem::path resolved_path = std::filesystem::weakly_canonical(
18+
std::filesystem::path(basedir) / std::filesystem::path(user_input));
19+
/* Ensure the resolved path is within our basedir */
20+
for (auto p = resolved_path; !p.empty(); p = p.parent_path()) {
21+
if (std::filesystem::equivalent(p, abs_base)) {
22+
return resolved_path;
23+
}
24+
}
25+
return {};
26+
}
27+
1328
cpp::result<void, std::string> FileFsRepository::StoreFile(
1429
OpenAi::File& file_metadata, const char* content, uint64_t length) {
1530
auto file_container_path = GetFilePath();
@@ -18,7 +33,11 @@ cpp::result<void, std::string> FileFsRepository::StoreFile(
1833
}
1934

2035
auto original_filename = file_metadata.filename;
21-
auto file_full_path = file_container_path / original_filename;
36+
auto file_full_path = SanitizePath(original_filename, file_container_path);
37+
38+
if (file_full_path.empty()) {
39+
return cpp::fail("Error resolving path in: " + original_filename);
40+
}
2241

2342
// Handle duplicate filenames
2443
int counter = 1;

engine/services/database_service.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,125 +6,161 @@ std::optional<EngineEntry> DatabaseService::UpsertEngine(
66
const std::string& api_key, const std::string& url,
77
const std::string& version, const std::string& variant,
88
const std::string& status, const std::string& metadata) {
9+
std::lock_guard<std::mutex> l(mtx_);
910
return cortex::db::Engines().UpsertEngine(engine_name, type, api_key, url,
1011
version, variant, status, metadata);
1112
}
1213

1314
std::optional<std::vector<EngineEntry>> DatabaseService::GetEngines() const {
15+
std::lock_guard<std::mutex> l(mtx_);
1416
return cortex::db::Engines().GetEngines();
1517
}
1618

1719
std::optional<EngineEntry> DatabaseService::GetEngineById(int id) const {
20+
std::lock_guard<std::mutex> l(mtx_);
1821
return cortex::db::Engines().GetEngineById(id);
1922
}
2023

2124
std::optional<EngineEntry> DatabaseService::GetEngineByNameAndVariant(
2225
const std::string& engine_name,
2326
const std::optional<std::string> variant) const {
27+
std::lock_guard<std::mutex> l(mtx_);
2428
return cortex::db::Engines().GetEngineByNameAndVariant(engine_name, variant);
2529
}
2630

2731
std::optional<std::string> DatabaseService::DeleteEngineById(int id) {
32+
std::lock_guard<std::mutex> l(mtx_);
2833
return cortex::db::Engines().DeleteEngineById(id);
2934
}
3035
// end engines
3136

3237
// begin file
3338
cpp::result<std::vector<OpenAi::File>, std::string>
3439
DatabaseService::GetFileList() const {
40+
std::lock_guard<std::mutex> l(mtx_);
3541
return cortex::db::File().GetFileList();
3642
}
3743

3844
cpp::result<OpenAi::File, std::string> DatabaseService::GetFileById(
3945
const std::string& file_id) const {
46+
std::lock_guard<std::mutex> l(mtx_);
4047
return cortex::db::File().GetFileById(file_id);
4148
}
4249

4350
cpp::result<void, std::string> DatabaseService::AddFileEntry(
4451
OpenAi::File& file) {
52+
std::lock_guard<std::mutex> l(mtx_);
4553
return cortex::db::File().AddFileEntry(file);
4654
}
4755

4856
cpp::result<void, std::string> DatabaseService::DeleteFileEntry(
4957
const std::string& file_id) {
58+
std::lock_guard<std::mutex> l(mtx_);
5059
return cortex::db::File().DeleteFileEntry(file_id);
5160
}
5261
// end file
5362

5463
// begin hardware
5564
cpp::result<std::vector<HardwareEntry>, std::string>
5665
DatabaseService::LoadHardwareList() const {
66+
std::lock_guard<std::mutex> l(mtx_);
5767
return cortex::db::Hardware().LoadHardwareList();
5868
}
5969

6070
cpp::result<bool, std::string> DatabaseService::AddHardwareEntry(
6171
const HardwareEntry& new_entry) {
72+
std::lock_guard<std::mutex> l(mtx_);
6273
return cortex::db::Hardware().AddHardwareEntry(new_entry);
6374
}
6475

76+
bool DatabaseService::HasHardwareEntry(const std::string& id) {
77+
std::lock_guard<std::mutex> l(mtx_);
78+
return cortex::db::Hardware().HasHardwareEntry(id);
79+
}
80+
6581
cpp::result<bool, std::string> DatabaseService::UpdateHardwareEntry(
6682
const std::string& id, const HardwareEntry& updated_entry) {
83+
std::lock_guard<std::mutex> l(mtx_);
6784
return cortex::db::Hardware().UpdateHardwareEntry(id, updated_entry);
6885
}
6986

7087
cpp::result<bool, std::string> DatabaseService::DeleteHardwareEntry(
7188
const std::string& id) {
89+
std::lock_guard<std::mutex> l(mtx_);
7290
return cortex::db::Hardware().DeleteHardwareEntry(id);
7391
}
92+
93+
cpp::result<bool, std::string> DatabaseService::UpdateHardwareEntry(
94+
const std::string& id, int hw_id, int sw_id) const {
95+
std::lock_guard<std::mutex> l(mtx_);
96+
return cortex::db::Hardware().UpdateHardwareEntry(id, hw_id, sw_id);
97+
}
7498
// end hardware
7599

76100
// begin models
77101
cpp::result<std::vector<ModelEntry>, std::string>
78102
DatabaseService::LoadModelList() const {
103+
std::lock_guard<std::mutex> l(mtx_);
79104
return cortex::db::Models().LoadModelList();
80105
}
81106

82107
cpp::result<ModelEntry, std::string> DatabaseService::GetModelInfo(
83108
const std::string& identifier) const {
109+
std::lock_guard<std::mutex> l(mtx_);
84110
return cortex::db::Models().GetModelInfo(identifier);
85111
}
86112

87113
cpp::result<bool, std::string> DatabaseService::AddModelEntry(
88114
ModelEntry new_entry) {
115+
std::lock_guard<std::mutex> l(mtx_);
89116
return cortex::db::Models().AddModelEntry(new_entry);
90117
}
91118

92119
cpp::result<bool, std::string> DatabaseService::UpdateModelEntry(
93120
const std::string& identifier, const ModelEntry& updated_entry) {
121+
std::lock_guard<std::mutex> l(mtx_);
94122
return cortex::db::Models().UpdateModelEntry(identifier, updated_entry);
95123
}
96124

97125
cpp::result<bool, std::string> DatabaseService::DeleteModelEntry(
98126
const std::string& identifier) {
127+
std::lock_guard<std::mutex> l(mtx_);
99128
return cortex::db::Models().DeleteModelEntry(identifier);
100129
}
101130

102131
cpp::result<bool, std::string> DatabaseService::DeleteModelEntryWithOrg(
103132
const std::string& src) {
133+
std::lock_guard<std::mutex> l(mtx_);
104134
return cortex::db::Models().DeleteModelEntryWithOrg(src);
105135
}
106136

107137
cpp::result<bool, std::string> DatabaseService::DeleteModelEntryWithRepo(
108138
const std::string& src) {
139+
140+
std::lock_guard<std::mutex> l(mtx_);
109141
return cortex::db::Models().DeleteModelEntryWithRepo(src);
110142
}
111143

112144
cpp::result<std::vector<std::string>, std::string>
113145
DatabaseService::FindRelatedModel(const std::string& identifier) const {
146+
std::lock_guard<std::mutex> l(mtx_);
114147
return cortex::db::Models().FindRelatedModel(identifier);
115148
}
116149

117150
bool DatabaseService::HasModel(const std::string& identifier) const {
151+
std::lock_guard<std::mutex> l(mtx_);
118152
return cortex::db::Models().HasModel(identifier);
119153
}
120154

121155
cpp::result<std::vector<ModelEntry>, std::string> DatabaseService::GetModels(
122156
const std::string& model_src) const {
157+
std::lock_guard<std::mutex> l(mtx_);
123158
return cortex::db::Models().GetModels(model_src);
124159
}
125160

126161
cpp::result<std::vector<ModelEntry>, std::string>
127162
DatabaseService::GetModelSources() const {
163+
std::lock_guard<std::mutex> l(mtx_);
128164
return cortex::db::Models().GetModelSources();
129165
}
130166
// end models

engine/services/database_service.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <mutex>
23
#include "database/engines.h"
34
#include "database/file.h"
45
#include "database/hardware.h"
@@ -39,9 +40,13 @@ class DatabaseService {
3940
cpp::result<std::vector<HardwareEntry>, std::string> LoadHardwareList() const;
4041
cpp::result<bool, std::string> AddHardwareEntry(
4142
const HardwareEntry& new_entry);
43+
bool HasHardwareEntry(const std::string& id);
4244
cpp::result<bool, std::string> UpdateHardwareEntry(
4345
const std::string& id, const HardwareEntry& updated_entry);
4446
cpp::result<bool, std::string> DeleteHardwareEntry(const std::string& id);
47+
cpp::result<bool, std::string> UpdateHardwareEntry(const std::string& id,
48+
int hw_id,
49+
int sw_id) const;
4550

4651
// models
4752
cpp::result<std::vector<ModelEntry>, std::string> LoadModelList() const;
@@ -62,8 +67,8 @@ class DatabaseService {
6267
bool HasModel(const std::string& identifier) const;
6368
cpp::result<std::vector<ModelEntry>, std::string> GetModels(
6469
const std::string& model_src) const;
65-
cpp::result<std::vector<ModelEntry>, std::string> GetModelSources()
66-
const;
70+
cpp::result<std::vector<ModelEntry>, std::string> GetModelSources() const;
6771

6872
private:
73+
mutable std::mutex mtx_;
6974
};

engine/services/hardware_service.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ void HardwareService::UpdateHardwareInfos() {
296296
using HwEntry = cortex::db::HardwareEntry;
297297
CheckDependencies();
298298
auto gpus = cortex::hw::GetGPUInfo();
299-
cortex::db::Hardware hw_db;
300-
auto b = hw_db.LoadHardwareList();
299+
auto b = db_service_->LoadHardwareList();
301300
// delete if not exists
302301
auto exists = [&gpus](const std::string& uuid) {
303302
for (auto const& g : gpus) {
@@ -308,12 +307,12 @@ void HardwareService::UpdateHardwareInfos() {
308307
};
309308
for (auto const& he : b.value()) {
310309
if (!exists(he.uuid)) {
311-
hw_db.DeleteHardwareEntry(he.uuid);
310+
db_service_->DeleteHardwareEntry(he.uuid);
312311
}
313312
}
314313

315314
// Get updated list
316-
b = hw_db.LoadHardwareList();
315+
b = db_service_->LoadHardwareList();
317316
std::vector<std::pair<int, int>> activated_gpu_bf;
318317
std::string debug_b;
319318
for (auto const& he : b.value()) {
@@ -326,15 +325,15 @@ void HardwareService::UpdateHardwareInfos() {
326325
for (auto const& gpu : gpus) {
327326
// ignore error
328327
// Note: only support NVIDIA for now, so hardware_id = software_id
329-
if (hw_db.HasHardwareEntry(gpu.uuid)) {
330-
auto res = hw_db.UpdateHardwareEntry(gpu.uuid, std::stoi(gpu.id),
328+
if (db_service_->HasHardwareEntry(gpu.uuid)) {
329+
auto res = db_service_->UpdateHardwareEntry(gpu.uuid, std::stoi(gpu.id),
331330
std::stoi(gpu.id));
332331
if (res.has_error()) {
333332
CTL_WRN(res.error());
334333
}
335334
} else {
336335
auto res =
337-
hw_db.AddHardwareEntry(HwEntry{.uuid = gpu.uuid,
336+
db_service_->AddHardwareEntry(HwEntry{.uuid = gpu.uuid,
338337
.type = "gpu",
339338
.hardware_id = std::stoi(gpu.id),
340339
.software_id = std::stoi(gpu.id),

engine/services/model_service.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,7 @@ cpp::result<StartModelResult, std::string> ModelService::StartModel(
858858
} else {
859859
// only report to user the error
860860
for (auto& depend : depends) {
861-
862-
StopModel(depend);
861+
(void)StopModel(depend);
863862
}
864863
}
865864
CTL_ERR("Model failed to start with status code: " << status);
@@ -869,7 +868,7 @@ cpp::result<StartModelResult, std::string> ModelService::StartModel(
869868

870869
// Running remote model
871870
if (engine_svc_->IsRemoteEngine(mc.engine)) {
872-
engine_svc_->LoadEngine(mc.engine);
871+
(void)engine_svc_->LoadEngine(mc.engine);
873872
config::RemoteModelConfig remote_mc;
874873
remote_mc.LoadFromYamlFile(
875874
fmu::ToAbsoluteCortexDataPath(
@@ -1035,7 +1034,7 @@ cpp::result<bool, std::string> ModelService::StopModel(
10351034
// Stop all depends model
10361035
auto depends = python_model_config.depends;
10371036
for (auto& depend : depends) {
1038-
StopModel(depend);
1037+
(void)StopModel(depend);
10391038
}
10401039
}
10411040

@@ -1233,7 +1232,7 @@ cpp::result<std::optional<std::string>, std::string>
12331232
ModelService::MayFallbackToCpu(const std::string& model_path, int ngl,
12341233
int ctx_len, int n_batch, int n_ubatch,
12351234
const std::string& kv_cache_type) {
1236-
// TODO(sang) temporary disable this function
1235+
// TODO(sang) temporary disable this function
12371236
return std::nullopt;
12381237
assert(hw_service_);
12391238
auto hw_info = hw_service_->GetHardwareInfo();

0 commit comments

Comments
 (0)