22#include " cmd_info.h"
33#include " config/yaml_config.h"
44#include " utils/file_manager_utils.h"
5+ #include " utils/modellist_utils.h"
56
67namespace commands {
7- bool ModelDelCmd::Exec (const std::string& model_id) {
8- // TODO this implentation may be changed after we have a decision
9- // on https://github.com/janhq/cortex.cpp/issues/1154 but the logic should be similar
10- CmdInfo ci (model_id);
11- std::string model_file =
12- ci.branch == " main" ? ci.model_name : ci.model_name + " -" + ci.branch ;
13- auto models_path = file_manager_utils::GetModelsContainerPath ();
14- if (std::filesystem::exists (models_path) &&
15- std::filesystem::is_directory (models_path)) {
16- // Iterate through directory
17- for (const auto & entry : std::filesystem::directory_iterator (models_path)) {
18- if (entry.is_regular_file () && entry.path ().extension () == " .yaml" ) {
19- try {
20- config::YamlHandler handler;
21- handler.ModelConfigFromFile (entry.path ().string ());
22- auto cfg = handler.GetModelConfig ();
23- if (entry.path ().stem ().string () == model_file) {
24- // Delete data
25- if (cfg.files .size () > 0 ) {
26- std::filesystem::path f (cfg.files [0 ]);
27- auto rel = std::filesystem::relative (f, models_path);
28- // Only delete model data if it is stored in our models folder
29- if (!rel.empty ()) {
30- if (cfg.engine == " cortex.llamacpp" ) {
31- std::filesystem::remove_all (f.parent_path ());
32- } else {
33- std::filesystem::remove_all (f);
34- }
35- }
36- }
8+ bool ModelDelCmd::Exec (const std::string& model_handle) {
9+ modellist_utils::ModelListUtils modellist_handler;
10+ config::YamlHandler yaml_handler;
3711
38- // Delete yaml file
39- std::filesystem::remove (entry);
40- CLI_LOG (" The model " << model_id << " was deleted" );
41- return true ;
12+ try {
13+ auto model_entry = modellist_handler.GetModelInfo (model_handle);
14+ yaml_handler.ModelConfigFromFile (model_entry.path_to_model_yaml );
15+ auto mc = yaml_handler.GetModelConfig ();
16+ // Remove yaml file
17+ std::filesystem::remove (model_entry.path_to_model_yaml );
18+ // Remove model files if they are not imported locally
19+ if (model_entry.branch_name != " imported" ) {
20+ if (mc.files .size () > 0 ) {
21+ if (mc.engine == " cortex.llamacpp" ) {
22+ for (auto & file : mc.files ) {
23+ std::filesystem::path gguf_p (file);
24+ std::filesystem::remove (gguf_p);
4225 }
43- } catch (const std::exception& e) {
44- CTL_WRN (" Error reading yaml file '" << entry.path ().string ()
45- << " ': " << e.what ());
46- return false ;
26+ } else {
27+ std::filesystem::path f (mc.files [0 ]);
28+ std::filesystem::remove_all (f);
4729 }
30+ } else {
31+ CTL_WRN (" model config files are empty!" );
4832 }
4933 }
50- }
51-
52- CLI_LOG (" Model does not exist: " << model_id);
5334
54- return false ;
35+ // update model.list
36+ if (modellist_handler.DeleteModelEntry (model_handle)) {
37+ CLI_LOG (" The model " << model_handle << " was deleted" );
38+ return true ;
39+ } else {
40+ CTL_ERR (" Could not delete model: " << model_handle);
41+ return false ;
42+ }
43+ } catch (const std::exception& e) {
44+ CLI_LOG (" Fail to delete model with ID '" + model_handle + " ': " + e.what ());
45+ false ;
46+ }
5547}
5648} // namespace commands
0 commit comments