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

Commit 69bb241

Browse files
committed
chore: consolidate download service
1 parent 0a3c5da commit 69bb241

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+559
-24796
lines changed

engine/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ find_package(LibArchive REQUIRED)
7878
find_package(tabulate CONFIG REQUIRED)
7979
find_package(CURL REQUIRED)
8080
find_package(SQLiteCpp REQUIRED)
81+
find_package(eventpp CONFIG REQUIRED)
8182

8283
add_executable(${TARGET_NAME} main.cc
8384
${CMAKE_CURRENT_SOURCE_DIR}/utils/cpuid/cpu_info.cc
@@ -94,6 +95,7 @@ target_link_libraries(${TARGET_NAME} PRIVATE CURL::libcurl)
9495
target_link_libraries(${TARGET_NAME} PRIVATE JsonCpp::JsonCpp Drogon::Drogon OpenSSL::SSL OpenSSL::Crypto yaml-cpp::yaml-cpp
9596
${CMAKE_THREAD_LIBS_INIT})
9697
target_link_libraries(${TARGET_NAME} PRIVATE SQLiteCpp)
98+
target_link_libraries(${TARGET_NAME} PRIVATE eventpp::eventpp)
9799

98100
# ##############################################################################
99101

engine/commands/chat_completion_cmd.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
#include "chat_completion_cmd.h"
2-
#include "httplib.h"
3-
42
#include "config/yaml_config.h"
53
#include "cortex_upd_cmd.h"
64
#include "database/models.h"
5+
#include "httplib.h"
76
#include "model_status_cmd.h"
8-
#include "run_cmd.h"
97
#include "server_start_cmd.h"
10-
#include "trantor/utils/Logger.h"
118
#include "utils/logging_utils.h"
129

1310
namespace commands {
@@ -78,7 +75,8 @@ void ChatCompletionCmd::Exec(const std::string& host, int port,
7875

7976
// Only check if llamacpp engine
8077
if ((mc.engine.find("llamacpp") != std::string::npos) &&
81-
!commands::ModelStatusCmd().IsLoaded(host, port, model_handle)) {
78+
!commands::ModelStatusCmd(model_service_)
79+
.IsLoaded(host, port, model_handle)) {
8280
CLI_LOG("Model is not loaded yet!");
8381
return;
8482
}
@@ -149,4 +147,4 @@ void ChatCompletionCmd::Exec(const std::string& host, int port,
149147
}
150148
}
151149

152-
}; // namespace commands
150+
}; // namespace commands

engine/commands/chat_completion_cmd.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
#include <vector>
44
#include "config/model_config.h"
55
#include "nlohmann/json.hpp"
6+
#include "services/model_service.h"
67

78
namespace commands {
89
class ChatCompletionCmd {
910
public:
11+
explicit ChatCompletionCmd(const ModelService& model_service)
12+
: model_service_{model_service} {};
13+
1014
void Exec(const std::string& host, int port, const std::string& model_handle,
1115
std::string msg);
1216
void Exec(const std::string& host, int port, const std::string& model_handle,
1317
const config::ModelConfig& mc, std::string msg);
1418

1519
private:
1620
std::vector<nlohmann::json> histories_;
21+
ModelService model_service_;
1722
};
18-
} // namespace commands
23+
} // namespace commands

engine/commands/cortex_upd_cmd.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets,
385385
.localPath = local_path,
386386
}}}};
387387

388-
DownloadService().AddDownloadTask(
388+
auto result = DownloadService().AddDownloadTask(
389389
download_task, [](const DownloadTask& finishedTask) {
390390
// try to unzip the downloaded file
391391
CTL_INF("Downloaded engine path: "
@@ -400,6 +400,9 @@ bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets,
400400

401401
CTL_INF("Finished!");
402402
});
403+
if (result.has_error()) {
404+
CTL_ERR("Failed to download: " << result.error());
405+
}
403406
break;
404407
}
405408
}
@@ -447,7 +450,7 @@ bool CortexUpdCmd::GetNightly(const std::string& v) {
447450
.localPath = localPath,
448451
}}};
449452

450-
DownloadService().AddDownloadTask(
453+
auto result = DownloadService().AddDownloadTask(
451454
download_task, [](const DownloadTask& finishedTask) {
452455
// try to unzip the downloaded file
453456
CTL_INF("Downloaded engine path: "
@@ -461,6 +464,10 @@ bool CortexUpdCmd::GetNightly(const std::string& v) {
461464

462465
CTL_INF("Finished!");
463466
});
467+
if (result.has_error()) {
468+
CTL_ERR("Failed to download: " << result.error());
469+
return false;
470+
}
464471

465472
// Replace binary file
466473
auto executable_path = file_manager_utils::GetExecutableFolderContainerPath();

engine/commands/engine_get_cmd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace commands {
66
class EngineGetCmd {
77
public:
8-
explicit EngineGetCmd() : engine_service_{EngineService()} {};
8+
explicit EngineGetCmd()
9+
: engine_service_{EngineService(std::make_shared<DownloadService>())} {};
910

1011
void Exec(const std::string& engineName) const;
1112

engine/commands/engine_install_cmd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ namespace commands {
77

88
class EngineInstallCmd {
99
public:
10-
explicit EngineInstallCmd() : engine_service_{EngineService()} {};
10+
explicit EngineInstallCmd()
11+
: engine_service_{EngineService(std::make_shared<DownloadService>())} {};
1112

1213
void Exec(const std::string& engine, const std::string& version = "latest",
1314
const std::string& src = "");

engine/commands/engine_list_cmd.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#include "engine_list_cmd.h"
22
#include <tabulate/table.hpp>
3-
#include "services/engine_service.h"
43

54
namespace commands {
65

76
bool EngineListCmd::Exec() {
8-
auto engine_service = EngineService();
9-
auto status_list = engine_service.GetEngineInfoList();
7+
auto status_list = engine_service_.GetEngineInfoList();
108

119
tabulate::Table table;
1210
table.add_row(

engine/commands/engine_list_cmd.h

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

3+
#include "services/engine_service.h"
4+
35
namespace commands {
46
class EngineListCmd {
57
public:
8+
explicit EngineListCmd()
9+
: engine_service_{EngineService(std::make_shared<DownloadService>())} {};
10+
611
bool Exec();
12+
13+
private:
14+
EngineService engine_service_;
715
};
816

917
} // namespace commands

engine/commands/engine_uninstall_cmd.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#pragma once
22

3+
#include <memory>
34
#include <string>
45
#include "services/engine_service.h"
56

67
namespace commands {
78
class EngineUninstallCmd {
89
public:
9-
explicit EngineUninstallCmd() : engine_service_{EngineService()} {};
10+
explicit EngineUninstallCmd()
11+
: engine_service_{EngineService(std::make_shared<DownloadService>())} {};
1012

1113
void Exec(const std::string& engine);
1214

engine/commands/model_del_cmd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ namespace commands {
77

88
class ModelDelCmd {
99
public:
10-
explicit ModelDelCmd() : model_service_{ModelService()} {};
10+
explicit ModelDelCmd()
11+
: model_service_{ModelService(std::make_shared<DownloadService>())} {};
1112

1213
void Exec(const std::string& model_handle);
1314

0 commit comments

Comments
 (0)