Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 34 additions & 42 deletions engine/commands/model_del_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,47 @@
#include "cmd_info.h"
#include "config/yaml_config.h"
#include "utils/file_manager_utils.h"
#include "utils/modellist_utils.h"

namespace commands {
bool ModelDelCmd::Exec(const std::string& model_id) {
// TODO this implentation may be changed after we have a decision
// on https://github.com/janhq/cortex.cpp/issues/1154 but the logic should be similar
CmdInfo ci(model_id);
std::string model_file =
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
auto models_path = file_manager_utils::GetModelsContainerPath();
if (std::filesystem::exists(models_path) &&
std::filesystem::is_directory(models_path)) {
// Iterate through directory
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
if (entry.is_regular_file() && entry.path().extension() == ".yaml") {
try {
config::YamlHandler handler;
handler.ModelConfigFromFile(entry.path().string());
auto cfg = handler.GetModelConfig();
if (entry.path().stem().string() == model_file) {
// Delete data
if (cfg.files.size() > 0) {
std::filesystem::path f(cfg.files[0]);
auto rel = std::filesystem::relative(f, models_path);
// Only delete model data if it is stored in our models folder
if (!rel.empty()) {
if (cfg.engine == "cortex.llamacpp") {
std::filesystem::remove_all(f.parent_path());
} else {
std::filesystem::remove_all(f);
}
}
}
bool ModelDelCmd::Exec(const std::string& model_handle) {
modellist_utils::ModelListUtils modellist_handler;
config::YamlHandler yaml_handler;

// Delete yaml file
std::filesystem::remove(entry);
CLI_LOG("The model " << model_id << " was deleted");
return true;
try {
auto model_entry = modellist_handler.GetModelInfo(model_handle);
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
auto mc = yaml_handler.GetModelConfig();
// Remove yaml file
std::filesystem::remove(model_entry.path_to_model_yaml);
// Remove model files if they are not imported locally
if (model_entry.branch_name != "imported") {
if (mc.files.size() > 0) {
if (mc.engine == "cortex.llamacpp") {
for (auto& file : mc.files) {
std::filesystem::path gguf_p(file);
std::filesystem::remove(gguf_p);
}
} catch (const std::exception& e) {
CTL_WRN("Error reading yaml file '" << entry.path().string()
<< "': " << e.what());
return false;
} else {
std::filesystem::path f(mc.files[0]);
std::filesystem::remove_all(f);
}
} else {
CTL_WRN("model config files are empty!");
}
}
}

CLI_LOG("Model does not exist: " << model_id);

return false;
// update model.list
if (modellist_handler.DeleteModelEntry(model_handle)) {
CLI_LOG("The model " << model_handle << " was deleted");
return true;
} else {
CTL_ERR("Could not delete model: " << model_handle);
return false;
}
} catch (const std::exception& e) {
CLI_LOG("Fail to delete model with ID '" + model_handle + "': " + e.what());
false;
}
}
} // namespace commands
2 changes: 1 addition & 1 deletion engine/commands/model_del_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace commands {

class ModelDelCmd {
public:
bool Exec(const std::string& model_id);
bool Exec(const std::string& model_handle);
};
}
Loading