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

Commit 0a3c5da

Browse files
authored
Merge pull request #1411 from janhq/j/add-read-remote-yaml
chore: add read remote yaml function
2 parents 4fcb40c + b6449ae commit 0a3c5da

File tree

6 files changed

+89
-24
lines changed

6 files changed

+89
-24
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ platform/package-lock.json
1919
.vscode
2020
platform/command
2121
platform/src/infrastructure/commanders/test/test_data
22-
**/vcpkg_installed
22+
**/vcpkg_installed
23+
engine/test.db

engine/commands/model_list_cmd.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace commands {
1212
void ModelListCmd::Exec() {
1313
namespace fs = std::filesystem;
1414
namespace fmu = file_manager_utils;
15-
auto models_path = file_manager_utils::GetModelsContainerPath();
1615
cortex::db::Models modellist_handler;
1716
config::YamlHandler yaml_handler;
1817
tabulate::Table table;

engine/commands/run_cmd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
2+
23
#include <string>
3-
#include "nlohmann/json.hpp"
44
#include "services/engine_service.h"
55
#include "services/model_service.h"
66

engine/database/models.cc

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#include "models.h"
22
#include <algorithm>
3-
#include <filesystem>
4-
#include <fstream>
53
#include <iostream>
64
#include <sstream>
7-
#include <stdexcept>
85
#include "database.h"
9-
#include "utils/file_manager_utils.h"
106
#include "utils/result.hpp"
117
#include "utils/scope_exit.h"
128

@@ -52,8 +48,7 @@ bool Models::IsUnique(const std::vector<ModelEntry>& entries,
5248
return std::none_of(
5349
entries.begin(), entries.end(), [&](const ModelEntry& entry) {
5450
return entry.model == model_id || entry.model_alias == model_id ||
55-
entry.model == model_alias ||
56-
entry.model_alias == model_alias;
51+
entry.model == model_alias || entry.model_alias == model_alias;
5752
});
5853
}
5954

@@ -186,8 +181,7 @@ cpp::result<bool, std::string> Models::AddModelEntry(ModelEntry new_entry,
186181
std::cout << "Test: " << model_list.error();
187182
return cpp::fail(model_list.error());
188183
}
189-
if (IsUnique(model_list.value(), new_entry.model,
190-
new_entry.model_alias)) {
184+
if (IsUnique(model_list.value(), new_entry.model, new_entry.model_alias)) {
191185
if (use_short_alias) {
192186
new_entry.model_alias =
193187
GenerateShortenedAlias(new_entry.model, model_list.value());

engine/utils/curl_utils.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <curl/curl.h>
2+
#include <nlohmann/json.hpp>
3+
#include <string>
4+
#include "utils/logging_utils.h"
5+
#include "utils/result.hpp"
6+
#include "yaml-cpp/yaml.h"
7+
8+
namespace curl_utils {
9+
namespace {
10+
size_t WriteCallback(void* contents, size_t size, size_t nmemb,
11+
std::string* output) {
12+
size_t totalSize = size * nmemb;
13+
output->append((char*)contents, totalSize);
14+
return totalSize;
15+
}
16+
} // namespace
17+
18+
inline cpp::result<std::string, std::string> SimpleGet(const std::string& url) {
19+
CURL* curl;
20+
CURLcode res;
21+
std::string readBuffer;
22+
23+
// Initialize libcurl
24+
curl_global_init(CURL_GLOBAL_DEFAULT);
25+
curl = curl_easy_init();
26+
27+
if (!curl) {
28+
return cpp::fail("Failed to init CURL");
29+
}
30+
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
31+
32+
// Set write function callback and data buffer
33+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
34+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
35+
36+
// Perform the request
37+
res = curl_easy_perform(curl);
38+
39+
if (res != CURLE_OK) {
40+
return cpp::fail("CURL request failed: " +
41+
static_cast<std::string>(curl_easy_strerror(res)));
42+
}
43+
44+
curl_easy_cleanup(curl);
45+
return readBuffer;
46+
}
47+
48+
inline cpp::result<YAML::Node, std::string> ReadRemoteYaml(
49+
const std::string& url) {
50+
auto result = SimpleGet(url);
51+
if (result.has_error()) {
52+
return cpp::fail(result.error());
53+
}
54+
55+
try {
56+
return YAML::Load(result.value());
57+
} catch (const std::exception& e) {
58+
return cpp::fail("YAML from " + url +
59+
" parsing error: " + std::string(e.what()));
60+
}
61+
}
62+
63+
inline cpp::result<nlohmann::json, std::string> SimpleGetJson(
64+
const std::string& url) {
65+
auto result = SimpleGet(url);
66+
if (result.has_error()) {
67+
return cpp::fail(result.error());
68+
}
69+
70+
try {
71+
return nlohmann::json::parse(result.value());
72+
} catch (const std::exception& e) {
73+
return cpp::fail("JSON from " + url +
74+
" parsing error: " + std::string(e.what()));
75+
}
76+
}
77+
} // namespace curl_utils

engine/utils/huggingface_utils.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

3-
#include <httplib.h>
43
#include <optional>
54
#include <string>
65
#include <vector>
6+
#include "utils/curl_utils.h"
77
#include "utils/json.hpp"
88
#include "utils/result.hpp"
99
#include "utils/url_parser.h"
@@ -58,17 +58,13 @@ GetModelRepositoryBranches(const std::string& author,
5858
.host = kHuggingfaceHost,
5959
.pathParams = {"api", "models", author, modelName, "refs"}};
6060

61-
httplib::Client cli(url_obj.GetProtocolAndHost());
62-
auto res = cli.Get(url_obj.GetPathAndQuery());
63-
if (res->status != httplib::StatusCode::OK_200) {
61+
auto result = curl_utils::SimpleGetJson(url_obj.ToFullPath());
62+
if (result.has_error()) {
6463
return cpp::fail("Failed to get model repository branches: " + author +
6564
"/" + modelName);
6665
}
6766

68-
using json = nlohmann::json;
69-
auto body = json::parse(res->body);
70-
auto branches_json = body["branches"];
71-
67+
auto branches_json = result.value()["branches"];
7268
std::vector<HuggingFaceBranch> branches{};
7369

7470
for (const auto& branch : branches_json) {
@@ -94,15 +90,13 @@ GetHuggingFaceModelRepoInfo(const std::string& author,
9490
.host = kHuggingfaceHost,
9591
.pathParams = {"api", "models", author, modelName}};
9692

97-
httplib::Client cli(url_obj.GetProtocolAndHost());
98-
auto res = cli.Get(url_obj.GetPathAndQuery());
99-
if (res->status != httplib::StatusCode::OK_200) {
93+
auto result = curl_utils::SimpleGetJson(url_obj.ToFullPath());
94+
if (result.has_error()) {
10095
return cpp::fail("Failed to get model repository info: " + author + "/" +
10196
modelName);
10297
}
10398

104-
using json = nlohmann::json;
105-
auto body = json::parse(res->body);
99+
auto body = result.value();
106100

107101
std::optional<HuggingFaceGgufInfo> gguf = std::nullopt;
108102
auto gguf_info = body["gguf"];

0 commit comments

Comments
 (0)