-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Nguyen
committed
Aug 27, 2024
1 parent
1b2e092
commit 0145f79
Showing
8 changed files
with
698 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include "engines.h" | ||
#include "utils/archive_utils.h" | ||
#include "utils/file_manager_utils.h" | ||
#include "utils/system_info_utils.h" | ||
|
||
void Engines::InitEngine(const HttpRequestPtr& req, | ||
std::function<void(const HttpResponsePtr&)>&& callback, | ||
const std::string& engine) const { | ||
LOG_DEBUG << "InitEngine, Engine: " << engine; | ||
if (engine.empty()) { | ||
Json::Value res; | ||
res["message"] = "Engine name is required"; | ||
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res); | ||
resp->setStatusCode(k409Conflict); | ||
callback(resp); | ||
LOG_WARN << "No engine field in path param"; | ||
return; | ||
} | ||
|
||
auto system_info = system_info_utils::GetSystemInfo(); | ||
if (system_info.arch == system_info_utils::kUnsupported || | ||
system_info.os == system_info_utils::kUnsupported) { | ||
Json::Value res; | ||
res["message"] = "Unsupported OS or architecture"; | ||
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res); | ||
resp->setStatusCode(k409Conflict); | ||
callback(resp); | ||
LOG_ERROR << "Unsupported OS or architecture: " << system_info.os << ", " | ||
<< system_info.arch; | ||
return; | ||
} | ||
|
||
using namespace nlohmann; | ||
|
||
// TODO: add more engines | ||
if (engine != "cortex.llamacpp") { | ||
Json::Value res; | ||
res["message"] = "Engine not supported"; | ||
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res); | ||
resp->setStatusCode(k400BadRequest); | ||
callback(resp); | ||
LOG_WARN << "Engine not supported"; | ||
return; | ||
} | ||
|
||
auto version{"latest"}; | ||
constexpr auto gitHubHost = "https://api.github.com"; | ||
|
||
std::ostringstream engineReleasePath; | ||
engineReleasePath << "/repos/janhq/" << engine << "/releases/" << version; | ||
|
||
httplib::Client cli(gitHubHost); | ||
if (auto res = cli.Get(engineReleasePath.str())) { | ||
if (res->status == httplib::StatusCode::OK_200) { | ||
try { | ||
auto jsonResponse = json::parse(res->body); | ||
auto assets = jsonResponse["assets"]; | ||
|
||
auto os_arch{system_info.os + "-" + system_info.arch}; | ||
for (auto& asset : assets) { | ||
auto assetName = asset["name"].get<std::string>(); | ||
if (assetName.find(os_arch) != std::string::npos) { | ||
std::string host{"https://github.com"}; | ||
|
||
auto full_url = asset["browser_download_url"].get<std::string>(); | ||
std::string path = full_url.substr(host.length()); | ||
|
||
auto fileName = asset["name"].get<std::string>(); | ||
LOG_INFO << "URL: " << full_url; | ||
|
||
auto downloadTask = DownloadTask{.id = engine, | ||
.type = DownloadType::Engine, | ||
.error = std::nullopt, | ||
.items = {DownloadItem{ | ||
.id = engine, | ||
.host = host, | ||
.fileName = fileName, | ||
.type = DownloadType::Engine, | ||
.path = path, | ||
}}}; | ||
|
||
DownloadService().AddAsyncDownloadTask( | ||
downloadTask, [](const std::string& absolute_path) { | ||
// try to unzip the downloaded file | ||
std::filesystem::path downloadedEnginePath{absolute_path}; | ||
LOG_INFO << "Downloaded engine path: " | ||
<< downloadedEnginePath.string(); | ||
|
||
archive_utils::ExtractArchive( | ||
downloadedEnginePath.string(), | ||
downloadedEnginePath.parent_path() | ||
.parent_path() | ||
.string()); | ||
|
||
// remove the downloaded file | ||
std::filesystem::remove(absolute_path); | ||
LOG_INFO << "Finished!"; | ||
}); | ||
|
||
Json::Value res; | ||
res["message"] = "Engine download started"; | ||
res["result"] = "OK"; | ||
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res); | ||
resp->setStatusCode(k200OK); | ||
callback(resp); | ||
return; | ||
} | ||
} | ||
Json::Value res; | ||
res["message"] = "Engine not found"; | ||
res["result"] = "Error"; | ||
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res); | ||
resp->setStatusCode(k404NotFound); | ||
callback(resp); | ||
} catch (const json::parse_error& e) { | ||
std::cerr << "JSON parse error: " << e.what() << std::endl; | ||
} | ||
} | ||
} else { | ||
auto err = res.error(); | ||
LOG_ERROR << "HTTP error: " << httplib::to_string(err); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include <drogon/HttpController.h> | ||
#include <trantor/utils/Logger.h> | ||
#include "services/download_service.h" | ||
#include "utils/cortex_utils.h" | ||
#include "utils/cortexso_parser.h" | ||
#include "utils/http_util.h" | ||
|
||
using namespace drogon; | ||
|
||
class Engines : public drogon::HttpController<Engines> { | ||
public: | ||
METHOD_LIST_BEGIN | ||
METHOD_ADD(Engines::InitEngine, "/{1}/init", Post); | ||
METHOD_LIST_END | ||
|
||
void InitEngine(const HttpRequestPtr& req, | ||
std::function<void(const HttpResponsePtr&)>&& callback, | ||
const std::string& engine) const; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <array> | ||
#include <cstdio> | ||
#include <iostream> | ||
#include <memory> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#ifdef _WIN32 | ||
#define POPEN _popen | ||
#define PCLOSE _pclose | ||
#else | ||
#define POPEN popen | ||
#define PCLOSE pclose | ||
#endif | ||
|
||
class CommandExecutor { | ||
public: | ||
CommandExecutor(const std::string& command) { | ||
FILE* pipe = POPEN(command.c_str(), "r"); | ||
if (!pipe) { | ||
throw std::runtime_error("popen() failed!"); | ||
} | ||
m_pipe = std::unique_ptr<FILE, decltype(&PCLOSE)>(pipe, PCLOSE); | ||
} | ||
|
||
CommandExecutor(const CommandExecutor&) = delete; | ||
CommandExecutor& operator=(const CommandExecutor&) = delete; | ||
CommandExecutor(CommandExecutor&&) = default; | ||
CommandExecutor& operator=(CommandExecutor&&) = default; | ||
~CommandExecutor() = default; | ||
|
||
std::string execute() { | ||
if (!m_pipe) { | ||
throw std::runtime_error("Command not initialized!"); | ||
} | ||
|
||
std::array<char, 128> buffer; | ||
std::string result; | ||
|
||
while (fgets(buffer.data(), buffer.size(), m_pipe.get()) != nullptr) { | ||
result += buffer.data(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private: | ||
std::unique_ptr<FILE, decltype(&PCLOSE)> m_pipe{nullptr, PCLOSE}; | ||
}; |
Oops, something went wrong.