1+ #include " model_del_cmd.h"
2+ #include " cmd_info.h"
3+ #include " config/yaml_config.h"
4+ #include " utils/file_manager_utils.h"
5+
6+ namespace 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+ }
37+
38+ // Delete yaml file
39+ std::filesystem::remove (entry);
40+ CLI_LOG (" The model " << model_id << " was deleted" );
41+ return true ;
42+ }
43+ } catch (const std::exception& e) {
44+ CTL_WRN (" Error reading yaml file '" << entry.path ().string ()
45+ << " ': " << e.what ());
46+ return false ;
47+ }
48+ }
49+ }
50+ }
51+
52+ CLI_LOG (" Model does not exist: " << model_id);
53+
54+ return false ;
55+ }
56+ } // namespace commands
0 commit comments