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

Commit c3837c5

Browse files
committed
update
1 parent a674b9c commit c3837c5

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

engine/cli/commands/engine_install_cmd.cc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,25 @@ bool EngineInstallCmd::Exec(const std::string& engine,
8484
std::vector<std::string> variant_selections;
8585
for (const auto& variant : variant_result.value()) {
8686
auto v_name = variant["name"].asString();
87-
if (string_utils::StringContainsIgnoreCase(v_name, hw_inf_.sys_inf->os)) {
87+
if (string_utils::StringContainsIgnoreCase(v_name, hw_inf_.sys_inf->os) &&
88+
string_utils::StringContainsIgnoreCase(v_name,
89+
hw_inf_.sys_inf->arch)) {
8890
variant_selections.push_back(variant["name"].asString());
8991
}
9092
}
91-
auto selected_variant =
92-
cli_selection_utils::PrintSelection(variant_selections);
93+
if (variant_selections.empty()) {
94+
CTL_ERR("No suitable variant found for " << hw_inf_.sys_inf->os << " "
95+
<< hw_inf_.sys_inf->arch);
96+
return false;
97+
}
98+
99+
std::optional<std::string> selected_variant = std::nullopt;
100+
if (variant_selections.size() == 1) {
101+
selected_variant = variant_selections[0];
102+
} else {
103+
selected_variant =
104+
cli_selection_utils::PrintSelection(variant_selections);
105+
}
93106
if (selected_variant == std::nullopt) {
94107
CTL_ERR("Invalid variant selection");
95108
return false;

engine/cli/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ int main(int argc, char* argv[]) {
171171
CLI_LOG(
172172
"New llama.cpp version available: " << config.latestLlamacppRelease);
173173
CLI_LOG("To update, run: " << commands::GetCortexBinary()
174-
<< " engines update llama-cpp");
174+
<< " engines update llama-cpp\n");
175175
}
176176
});
177177
t1.detach();

engine/services/engine_service.cc

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ cpp::result<bool, std::string> EngineService::UninstallEngineVariant(
181181
const std::string& engine, const std::optional<std::string> version,
182182
const std::optional<std::string> variant) {
183183
auto ne = NormalizeEngine(engine);
184+
if (IsEngineLoaded(ne)) {
185+
CTL_INF("Engine " << ne << " is already loaded, unloading it");
186+
auto unload_res = UnloadEngine(ne);
187+
if (unload_res.has_error()) {
188+
CTL_INF("Failed to unload engine: " << unload_res.error());
189+
return cpp::fail(unload_res.error());
190+
} else {
191+
CTL_INF("Engine " << ne << " unloaded successfully");
192+
}
193+
}
184194

185195
std::optional<std::filesystem::path> path_to_remove = std::nullopt;
186196
if (version == std::nullopt && variant == std::nullopt) {
@@ -264,6 +274,16 @@ cpp::result<void, std::string> EngineService::DownloadEngineV2(
264274
if (selected_variant == std::nullopt) {
265275
return cpp::fail("Failed to find a suitable variant for " + engine);
266276
}
277+
if (IsEngineLoaded(engine)) {
278+
CTL_INF("Engine " << engine << " is already loaded, unloading it");
279+
auto unload_res = UnloadEngine(engine);
280+
if (unload_res.has_error()) {
281+
CTL_INF("Failed to unload engine: " << unload_res.error());
282+
return cpp::fail(unload_res.error());
283+
} else {
284+
CTL_INF("Engine " << engine << " unloaded successfully");
285+
}
286+
}
267287
auto normalize_version = "v" + selected_variant->version;
268288

269289
auto variant_folder_name = engine_matcher_utils::GetVariantFromNameAndVersion(
@@ -276,7 +296,7 @@ cpp::result<void, std::string> EngineService::DownloadEngineV2(
276296
auto variant_path = variant_folder_path / selected_variant->name;
277297
std::filesystem::create_directories(variant_folder_path);
278298
CLI_LOG("variant_folder_path: " + variant_folder_path.string());
279-
auto on_finished = [this, engine, selected_variant,
299+
auto on_finished = [this, engine, selected_variant, variant_folder_path,
280300
normalize_version](const DownloadTask& finishedTask) {
281301
// try to unzip the downloaded file
282302
CLI_LOG("Engine zip path: " << finishedTask.items[0].localPath.string());
@@ -299,6 +319,22 @@ cpp::result<void, std::string> EngineService::DownloadEngineV2(
299319
CTL_INF("Set default engine variant: " << res.value().variant);
300320
}
301321

322+
// remove other engines
323+
auto engine_directories = file_manager_utils::GetEnginesContainerPath() /
324+
engine / selected_variant->name;
325+
326+
for (const auto& entry : std::filesystem::directory_iterator(
327+
variant_folder_path.parent_path())) {
328+
if (entry.is_directory() &&
329+
entry.path().filename() != normalize_version) {
330+
try {
331+
std::filesystem::remove_all(entry.path());
332+
} catch (const std::exception& e) {
333+
CTL_WRN("Could not delete directory: " << e.what());
334+
}
335+
}
336+
}
337+
302338
// remove the downloaded file
303339
try {
304340
std::filesystem::remove(finishedTask.items[0].localPath);
@@ -364,6 +400,16 @@ cpp::result<bool, std::string> EngineService::DownloadEngine(
364400
CTL_INF("Creating " << engine_folder_path.string());
365401
std::filesystem::create_directories(engine_folder_path);
366402
}
403+
if (IsEngineLoaded(engine)) {
404+
CTL_INF("Engine " << engine << " is already loaded, unloading it");
405+
auto unload_res = UnloadEngine(engine);
406+
if (unload_res.has_error()) {
407+
CTL_INF("Failed to unload engine: " << unload_res.error());
408+
return cpp::fail(unload_res.error());
409+
} else {
410+
CTL_INF("Engine " << engine << " unloaded successfully");
411+
}
412+
}
367413
CTL_INF("Engine folder path: " << engine_folder_path.string() << "\n");
368414
auto local_path = engine_folder_path / asset.name;
369415
auto downloadTask{
@@ -918,6 +964,17 @@ cpp::result<EngineUpdateResult, std::string> EngineService::UpdateEngine(
918964
CTL_INF("Default variant: " << default_variant->variant
919965
<< ", version: " + default_variant->version);
920966

967+
if (IsEngineLoaded(ne)) {
968+
CTL_INF("Engine " << ne << " is already loaded, unloading it");
969+
auto unload_res = UnloadEngine(ne);
970+
if (unload_res.has_error()) {
971+
CTL_INF("Failed to unload engine: " << unload_res.error());
972+
return cpp::fail(unload_res.error());
973+
} else {
974+
CTL_INF("Engine " << ne << " unloaded successfully");
975+
}
976+
}
977+
921978
auto latest_version = GetLatestEngineVersion(ne);
922979
if (latest_version.has_error()) {
923980
// if can't get latest version, stop

0 commit comments

Comments
 (0)