11#include " engine_service.h"
22#include < httplib.h>
3- #include < stdexcept>
43#include " algorithm"
54#include " utils/archive_utils.h"
65#include " utils/engine_matcher_utils.h"
76#include " utils/file_manager_utils.h"
87#include " utils/json.hpp"
8+ #include " utils/result.hpp"
99#include " utils/semantic_version_utils.h"
1010#include " utils/system_info_utils.h"
1111#include " utils/url_parser.h"
@@ -40,7 +40,6 @@ EngineService::~EngineService() {}
4040
4141std::optional<EngineInfo> EngineService::GetEngineInfo (
4242 const std::string& engine) const {
43- // if engine is not found in kSupportEngine, throw runtime error
4443 if (std::find (kSupportEngines .begin (), kSupportEngines .end (), engine) ==
4544 kSupportEngines .end ()) {
4645 return std::nullopt ;
@@ -99,21 +98,24 @@ std::vector<EngineInfo> EngineService::GetEngineInfoList() const {
9998 return engines;
10099}
101100
102- void EngineService::InstallEngine ( const std::string& engine,
103- const std::string& version,
104- const std::string& src) {
101+ cpp::result< void , std::string> EngineService::InstallEngine (
102+ const std::string& engine, const std::string& version,
103+ const std::string& src) {
105104
106105 if (!src.empty ()) {
107- UnzipEngine (engine, version, src);
106+ return UnzipEngine (engine, version, src);
108107 } else {
109- DownloadEngine (engine, version);
110- DownloadCuda (engine);
108+ auto result = DownloadEngine (engine, version);
109+ if (result.has_error ()) {
110+ return result;
111+ }
112+ return DownloadCuda (engine);
111113 }
112114}
113115
114- void EngineService::UnzipEngine ( const std::string& engine,
115- const std::string& version,
116- const std::string& path) {
116+ cpp::result< void , std::string> EngineService::UnzipEngine (
117+ const std::string& engine, const std::string& version,
118+ const std::string& path) {
117119 bool found_cuda = false ;
118120
119121 CTL_INF (" engine: " << engine);
@@ -150,9 +152,8 @@ void EngineService::UnzipEngine(const std::string& engine,
150152 }
151153 }
152154 } else {
153- // Folder does not exist, throw exception
154- CTL_ERR (" Folder does not exist: " << path);
155- return ;
155+ // Folder does not exist
156+ return cpp::fail (" Folder does not exist: " + path);
156157 }
157158
158159 auto matched_variant = GetMatchedVariant (engine, variants);
@@ -162,7 +163,7 @@ void EngineService::UnzipEngine(const std::string& engine,
162163 << hw_inf_.sys_inf ->arch
163164 << " , will get engine from remote" );
164165 // Go with the remote flow
165- DownloadEngine (engine, version);
166+ return DownloadEngine (engine, version);
166167 } else {
167168 auto engine_path = file_manager_utils::GetEnginesContainerPath ();
168169 archive_utils::ExtractArchive (path + " /" + matched_variant,
@@ -171,33 +172,33 @@ void EngineService::UnzipEngine(const std::string& engine,
171172
172173 // Not match any cuda binary, download from remote
173174 if (!found_cuda) {
174- DownloadCuda (engine);
175+ return DownloadCuda (engine);
175176 }
176- }
177-
178- void EngineService::UninstallEngine (const std::string& engine) {
179- // TODO: Unload the model which is currently running on engine_
180177
181- // TODO: Unload engine if is loaded
178+ return {};
179+ }
182180
181+ cpp::result<void , std::string> EngineService::UninstallEngine (
182+ const std::string& engine) {
183183 auto ecp = file_manager_utils::GetEnginesContainerPath ();
184184 auto engine_path = ecp / engine;
185185
186186 if (!std::filesystem::exists (engine_path)) {
187- throw std::runtime_error (" Engine " + engine + " is not installed!" );
187+ return cpp::fail (" Engine " + engine + " is not installed!" );
188188 }
189189
190190 try {
191191 std::filesystem::remove_all (engine_path);
192192 CTL_INF (" Engine " << engine << " uninstalled successfully!" );
193+ return {};
193194 } catch (const std::exception& e) {
194195 CTL_ERR (" Failed to uninstall engine " << engine << " : " << e.what ());
195- throw ;
196+ return cpp::fail ( " Failed to uninstall engine " + engine + " : " + e. what ()) ;
196197 }
197198}
198199
199- void EngineService::DownloadEngine ( const std::string& engine,
200- const std::string& version) {
200+ cpp::result< void , std::string> EngineService::DownloadEngine (
201+ const std::string& engine, const std::string& version) {
201202 auto get_params = [&engine, &version]() -> std::vector<std::string> {
202203 if (version == " latest" ) {
203204 return {" repos" , " janhq" , engine, " releases" , version};
@@ -231,7 +232,7 @@ void EngineService::DownloadEngine(const std::string& engine,
231232 body = get_data (body);
232233 }
233234 if (body.empty ()) {
234- throw std::runtime_error (" No release found for " + version);
235+ return cpp::fail (" No release found for " + version);
235236 }
236237
237238 auto assets = body[" assets" ];
@@ -249,7 +250,7 @@ void EngineService::DownloadEngine(const std::string& engine,
249250 CTL_INF (" Matched variant: " << matched_variant);
250251 if (matched_variant.empty ()) {
251252 CTL_ERR (" No variant found for " << os_arch);
252- throw std::runtime_error (" No variant found for " + os_arch);
253+ return cpp::fail (" No variant found for " + os_arch);
253254 }
254255
255256 for (auto & asset : assets) {
@@ -280,8 +281,7 @@ void EngineService::DownloadEngine(const std::string& engine,
280281 .localPath = local_path,
281282 }}}};
282283
283- DownloadService download_service;
284- download_service.AddDownloadTask (
284+ return DownloadService ().AddDownloadTask (
285285 downloadTask, [](const DownloadTask& finishedTask) {
286286 // try to unzip the downloaded file
287287 CTL_INF (" Engine zip path: "
@@ -302,23 +302,24 @@ void EngineService::DownloadEngine(const std::string& engine,
302302 }
303303 CTL_INF (" Finished!" );
304304 });
305- return ;
306305 }
307306 }
307+ return {};
308308 } else {
309- throw std::runtime_error (" Failed to fetch engine release: " + engine);
309+ return cpp::fail (" Failed to fetch engine release: " + engine);
310310 }
311311}
312312
313- void EngineService::DownloadCuda (const std::string& engine) {
313+ cpp::result<void , std::string> EngineService::DownloadCuda (
314+ const std::string& engine) {
314315 if (hw_inf_.sys_inf ->os == " mac" || engine == " cortex.onnx" ) {
315316 // mac and onnx engine does not require cuda toolkit
316- return ;
317+ return {} ;
317318 }
318319
319320 if (hw_inf_.cuda_driver_version .empty ()) {
320321 CTL_WRN (" No cuda driver, continue with CPU" );
321- return ;
322+ return {} ;
322323 }
323324 // download cuda toolkit
324325 const std::string jan_host = " catalog.jan.ai" ;
@@ -336,7 +337,7 @@ void EngineService::DownloadCuda(const std::string& engine) {
336337 << hw_inf_.cuda_driver_version
337338 << " is not compatible with cuda toolkit version "
338339 << suitable_toolkit_version);
339- throw std::runtime_error (" Cuda driver is not compatible with cuda toolkit" );
340+ return cpp::fail (" Cuda driver is not compatible with cuda toolkit" );
340341 }
341342
342343 auto url_obj = url_parser::Url{
@@ -362,8 +363,7 @@ void EngineService::DownloadCuda(const std::string& engine) {
362363 .localPath = cuda_toolkit_local_path}},
363364 }};
364365
365- DownloadService download_service;
366- download_service.AddDownloadTask (
366+ return DownloadService ().AddDownloadTask (
367367 downloadCudaToolkitTask, [&](const DownloadTask& finishedTask) {
368368 auto engine_path =
369369 file_manager_utils::GetEnginesContainerPath () / engine;
@@ -395,4 +395,4 @@ std::string EngineService::GetMatchedVariant(
395395 hw_inf_.cuda_driver_version );
396396 }
397397 return matched_variant;
398- }
398+ }
0 commit comments