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

Commit 668af84

Browse files
committed
Merge branch 'dev' of https://github.com/janhq/cortex.cpp into s/feat/spawn-llama-cpp
2 parents 0968abe + 8621376 commit 668af84

File tree

3 files changed

+84
-224
lines changed

3 files changed

+84
-224
lines changed

engine/cli/command_line_parser.cc

Lines changed: 82 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ CommandLineParser::CommandLineParser()
5353
db_service_{std::make_shared<DatabaseService>()},
5454
engine_service_{std::make_shared<EngineService>(
5555
download_service_, dylib_path_manager_, db_service_,
56-
std::make_shared<cortex::TaskQueue>(1, "q"))} {
57-
supported_engines_ = engine_service_->GetSupportedEngineNames().value();
58-
}
56+
std::make_shared<cortex::TaskQueue>(1, "q"))} {}
5957

6058
bool CommandLineParser::SetupCommand(int argc, char** argv) {
6159
app_.usage("Usage:\n" + commands::GetCortexBinary() +
@@ -484,104 +482,141 @@ void CommandLineParser::SetupEngineCommands() {
484482
install_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
485483
" engines install [engine_name] [options]");
486484
install_cmd->group(kSubcommands);
485+
install_cmd
486+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
487+
->required();
488+
install_cmd->add_option("-v, --version", cml_data_.engine_version,
489+
"Engine version to download");
490+
install_cmd->add_option("-s, --source", cml_data_.engine_src,
491+
"Install engine by local path");
492+
install_cmd->add_flag("-m, --menu", cml_data_.show_menu,
493+
"Display menu for engine variant selection");
494+
487495
install_cmd->callback([this, install_cmd] {
488496
if (std::exchange(executed_, true))
489497
return;
490-
if (install_cmd->get_subcommands().empty()) {
491-
CLI_LOG("[engine_name] is required\n");
492-
CLI_LOG(install_cmd->help());
498+
try {
499+
commands::EngineInstallCmd(
500+
engine_service_, cml_data_.config.apiServerHost,
501+
std::stoi(cml_data_.config.apiServerPort), cml_data_.show_menu)
502+
.Exec(cml_data_.engine_name, cml_data_.engine_version,
503+
cml_data_.engine_src);
504+
} catch (const std::exception& e) {
505+
CTL_ERR(e.what());
493506
}
494507
});
495508

496-
for (const auto& engine : supported_engines_) {
497-
EngineInstall(install_cmd, engine, cml_data_.engine_version,
498-
cml_data_.engine_src);
499-
}
500-
501509
auto uninstall_cmd =
502510
engines_cmd->add_subcommand("uninstall", "Uninstall engine");
503511
uninstall_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
504512
" engines uninstall [engine_name] [options]");
513+
uninstall_cmd->group(kSubcommands);
514+
uninstall_cmd
515+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
516+
->required();
505517
uninstall_cmd->callback([this, uninstall_cmd] {
506518
if (std::exchange(executed_, true))
507519
return;
508-
if (uninstall_cmd->get_subcommands().empty()) {
509-
CLI_LOG("[engine_name] is required\n");
510-
CLI_LOG(uninstall_cmd->help());
520+
try {
521+
commands::EngineUninstallCmd().Exec(
522+
cml_data_.config.apiServerHost,
523+
std::stoi(cml_data_.config.apiServerPort), cml_data_.engine_name);
524+
} catch (const std::exception& e) {
525+
CTL_ERR(e.what());
511526
}
512527
});
513-
uninstall_cmd->group(kSubcommands);
514-
for (const auto& engine : supported_engines_) {
515-
EngineUninstall(uninstall_cmd, engine);
516-
}
517528

518529
auto engine_upd_cmd = engines_cmd->add_subcommand("update", "Update engine");
519530
engine_upd_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
520531
" engines update [engine_name]");
532+
engine_upd_cmd->group(kSubcommands);
533+
engine_upd_cmd
534+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
535+
->required();
521536
engine_upd_cmd->callback([this, engine_upd_cmd] {
522537
if (std::exchange(executed_, true))
523538
return;
524-
if (engine_upd_cmd->get_subcommands().empty()) {
525-
CLI_LOG("[engine_name] is required\n");
526-
CLI_LOG(engine_upd_cmd->help());
539+
try {
540+
commands::EngineUpdateCmd().Exec(
541+
cml_data_.config.apiServerHost,
542+
std::stoi(cml_data_.config.apiServerPort), cml_data_.engine_name);
543+
} catch (const std::exception& e) {
544+
CTL_ERR(e.what());
527545
}
528546
});
529-
engine_upd_cmd->group(kSubcommands);
530-
for (const auto& engine : supported_engines_) {
531-
EngineUpdate(engine_upd_cmd, engine);
532-
}
533547

534548
auto engine_use_cmd =
535549
engines_cmd->add_subcommand("use", "Set engine as default");
536550
engine_use_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
537551
" engines use [engine_name]");
552+
engine_use_cmd->group(kSubcommands);
553+
engine_use_cmd
554+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
555+
->required();
538556
engine_use_cmd->callback([this, engine_use_cmd] {
539557
if (std::exchange(executed_, true))
540558
return;
541-
if (engine_use_cmd->get_subcommands().empty()) {
542-
CLI_LOG("[engine_name] is required\n");
543-
CLI_LOG(engine_use_cmd->help());
559+
auto result = commands::EngineUseCmd().Exec(
560+
cml_data_.config.apiServerHost,
561+
std::stoi(cml_data_.config.apiServerPort), cml_data_.engine_name);
562+
if (result.has_error()) {
563+
CTL_ERR(result.error());
564+
} else {
565+
CTL_INF("Engine " << cml_data_.engine_name << " is set as default");
544566
}
545567
});
546-
engine_use_cmd->group(kSubcommands);
547-
for (const auto& engine : supported_engines_) {
548-
EngineUse(engine_use_cmd, engine);
549-
}
550568

551569
auto engine_load_cmd = engines_cmd->add_subcommand("load", "Load engine");
552570
engine_load_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
553571
" engines load [engine_name]");
572+
engine_load_cmd->group(kSubcommands);
573+
engine_load_cmd
574+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
575+
->required();
554576
engine_load_cmd->callback([this, engine_load_cmd] {
555577
if (std::exchange(executed_, true))
556578
return;
557-
if (engine_load_cmd->get_subcommands().empty()) {
558-
CLI_LOG("[engine_name] is required\n");
559-
CLI_LOG(engine_load_cmd->help());
579+
auto result = commands::EngineLoadCmd().Exec(
580+
cml_data_.config.apiServerHost,
581+
std::stoi(cml_data_.config.apiServerPort), cml_data_.engine_name);
582+
if (result.has_error()) {
583+
CTL_ERR(result.error());
560584
}
561585
});
562-
engine_load_cmd->group(kSubcommands);
563-
for (const auto& engine : supported_engines_) {
564-
EngineLoad(engine_load_cmd, engine);
565-
}
566586

567587
auto engine_unload_cmd =
568588
engines_cmd->add_subcommand("unload", "Unload engine");
569589
engine_unload_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
570590
" engines unload [engine_name]");
591+
engine_unload_cmd->group(kSubcommands);
592+
engine_unload_cmd
593+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
594+
->required();
571595
engine_unload_cmd->callback([this, engine_unload_cmd] {
572596
if (std::exchange(executed_, true))
573597
return;
574-
if (engine_unload_cmd->get_subcommands().empty()) {
575-
CLI_LOG("[engine_name] is required\n");
576-
CLI_LOG(engine_unload_cmd->help());
598+
auto result = commands::EngineUnloadCmd().Exec(
599+
cml_data_.config.apiServerHost,
600+
std::stoi(cml_data_.config.apiServerPort), cml_data_.engine_name);
601+
if (result.has_error()) {
602+
CTL_ERR(result.error());
577603
}
578604
});
579-
engine_unload_cmd->group(kSubcommands);
580-
for (const auto& engine : supported_engines_) {
581-
EngineUnload(engine_unload_cmd, engine);
582-
}
583605

584-
EngineGet(engines_cmd);
606+
auto engine_get_cmd = engines_cmd->add_subcommand("get", "Get engine info");
607+
engine_get_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
608+
" engines get [engine_name] [options]");
609+
engine_get_cmd->group(kSubcommands);
610+
engine_get_cmd
611+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
612+
->required();
613+
engine_get_cmd->callback([this, engine_get_cmd] {
614+
if (std::exchange(executed_, true))
615+
return;
616+
commands::EngineGetCmd().Exec(cml_data_.config.apiServerHost,
617+
std::stoi(cml_data_.config.apiServerPort),
618+
cml_data_.engine_name);
619+
});
585620
}
586621

587622
void CommandLineParser::SetupHardwareCommands() {
@@ -739,167 +774,6 @@ void CommandLineParser::SetupSystemCommands() {
739774
});
740775
}
741776

742-
void CommandLineParser::EngineInstall(CLI::App* parent,
743-
const std::string& engine_name,
744-
std::string& version, std::string& src) {
745-
auto install_engine_cmd = parent->add_subcommand(engine_name, "");
746-
install_engine_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
747-
" engines install " + engine_name + " [options]");
748-
install_engine_cmd->group(kEngineGroup);
749-
750-
install_engine_cmd->add_option("-v, --version", version,
751-
"Engine version to download");
752-
753-
install_engine_cmd->add_option("-s, --source", src,
754-
"Install engine by local path");
755-
756-
install_engine_cmd->add_flag("-m, --menu", cml_data_.show_menu,
757-
"Display menu for engine variant selection");
758-
759-
install_engine_cmd->callback([this, engine_name, &version, &src] {
760-
if (std::exchange(executed_, true))
761-
return;
762-
try {
763-
commands::EngineInstallCmd(
764-
engine_service_, cml_data_.config.apiServerHost,
765-
std::stoi(cml_data_.config.apiServerPort), cml_data_.show_menu)
766-
.Exec(engine_name, version, src);
767-
} catch (const std::exception& e) {
768-
CTL_ERR(e.what());
769-
}
770-
});
771-
}
772-
773-
void CommandLineParser::EngineUninstall(CLI::App* parent,
774-
const std::string& engine_name) {
775-
auto uninstall_engine_cmd = parent->add_subcommand(engine_name, "");
776-
uninstall_engine_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
777-
" engines install " + engine_name + " [options]");
778-
uninstall_engine_cmd->group(kEngineGroup);
779-
780-
uninstall_engine_cmd->callback([this, engine_name] {
781-
if (std::exchange(executed_, true))
782-
return;
783-
try {
784-
commands::EngineUninstallCmd().Exec(
785-
cml_data_.config.apiServerHost,
786-
std::stoi(cml_data_.config.apiServerPort), engine_name);
787-
} catch (const std::exception& e) {
788-
CTL_ERR(e.what());
789-
}
790-
});
791-
}
792-
793-
void CommandLineParser::EngineUpdate(CLI::App* parent,
794-
const std::string& engine_name) {
795-
auto engine_update_cmd = parent->add_subcommand(engine_name, "");
796-
engine_update_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
797-
" engines update " + engine_name);
798-
engine_update_cmd->group(kEngineGroup);
799-
800-
engine_update_cmd->callback([this, engine_name] {
801-
if (std::exchange(executed_, true))
802-
return;
803-
try {
804-
commands::EngineUpdateCmd().Exec(
805-
cml_data_.config.apiServerHost,
806-
std::stoi(cml_data_.config.apiServerPort), engine_name);
807-
} catch (const std::exception& e) {
808-
CTL_ERR(e.what());
809-
}
810-
});
811-
}
812-
813-
void CommandLineParser::EngineUnload(CLI::App* parent,
814-
const std::string& engine_name) {
815-
auto sub_cmd = parent->add_subcommand(engine_name, "");
816-
sub_cmd->usage("Usage:\n" + commands::GetCortexBinary() + " engines unload " +
817-
engine_name);
818-
sub_cmd->group(kEngineGroup);
819-
820-
sub_cmd->callback([this, engine_name] {
821-
if (std::exchange(executed_, true))
822-
return;
823-
auto result = commands::EngineUnloadCmd().Exec(
824-
cml_data_.config.apiServerHost,
825-
std::stoi(cml_data_.config.apiServerPort), engine_name);
826-
if (result.has_error()) {
827-
CTL_ERR(result.error());
828-
}
829-
});
830-
}
831-
832-
void CommandLineParser::EngineLoad(CLI::App* parent,
833-
const std::string& engine_name) {
834-
auto sub_cmd = parent->add_subcommand(engine_name, "");
835-
sub_cmd->usage("Usage:\n" + commands::GetCortexBinary() + " engines load " +
836-
engine_name);
837-
sub_cmd->group(kEngineGroup);
838-
839-
sub_cmd->callback([this, engine_name] {
840-
if (std::exchange(executed_, true))
841-
return;
842-
auto result = commands::EngineLoadCmd().Exec(
843-
cml_data_.config.apiServerHost,
844-
std::stoi(cml_data_.config.apiServerPort), engine_name);
845-
if (result.has_error()) {
846-
CTL_ERR(result.error());
847-
}
848-
});
849-
}
850-
851-
void CommandLineParser::EngineUse(CLI::App* parent,
852-
const std::string& engine_name) {
853-
auto engine_use_cmd = parent->add_subcommand(engine_name, "");
854-
engine_use_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
855-
" engines use " + engine_name);
856-
engine_use_cmd->group(kEngineGroup);
857-
858-
engine_use_cmd->callback([this, engine_name] {
859-
if (std::exchange(executed_, true))
860-
return;
861-
auto result = commands::EngineUseCmd().Exec(
862-
cml_data_.config.apiServerHost,
863-
std::stoi(cml_data_.config.apiServerPort), engine_name);
864-
if (result.has_error()) {
865-
CTL_ERR(result.error());
866-
} else {
867-
CTL_INF("Engine " << engine_name << " is set as default");
868-
}
869-
});
870-
}
871-
872-
void CommandLineParser::EngineGet(CLI::App* parent) {
873-
auto get_cmd = parent->add_subcommand("get", "Get engine info");
874-
get_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
875-
" engines get [engine_name] [options]");
876-
get_cmd->group(kSubcommands);
877-
get_cmd->callback([this, get_cmd] {
878-
if (std::exchange(executed_, true))
879-
return;
880-
if (get_cmd->get_subcommands().empty()) {
881-
CLI_LOG("[engine_name] is required\n");
882-
CLI_LOG(get_cmd->help());
883-
}
884-
});
885-
886-
for (const auto& engine : supported_engines_) {
887-
std::string desc = "Get " + engine + " status";
888-
889-
auto engine_get_cmd = get_cmd->add_subcommand(engine, desc);
890-
engine_get_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
891-
" engines get " + engine + " [options]");
892-
engine_get_cmd->group(kEngineGroup);
893-
engine_get_cmd->callback([this, engine] {
894-
if (std::exchange(executed_, true))
895-
return;
896-
commands::EngineGetCmd().Exec(cml_data_.config.apiServerHost,
897-
std::stoi(cml_data_.config.apiServerPort),
898-
engine);
899-
});
900-
}
901-
}
902-
903777
void CommandLineParser::ModelUpdate(CLI::App* parent) {
904778
auto model_update_cmd =
905779
parent->add_subcommand("update", "Update model configurations");

0 commit comments

Comments
 (0)