\brief Save the history \param input_list, std::vector
| 443 | /// \brief Save the history |
| 444 | /// \param input_list, std::vector<std::string> |
| 445 | void Runner::cmd_save(std::vector<std::string>& input_list) { |
| 446 | std::pair<std::string, std::vector<int>> history = this->auto_chat_engine->get_history(); |
| 447 | // Get the FLM_MODEL_PATH environment variable for the history directory |
| 448 | std::string history_dir; |
| 449 | const char* path_sep = "/"; |
| 450 | #ifdef _WIN32 |
| 451 | path_sep = "\\"; |
| 452 | char* model_path_env = nullptr; |
| 453 | size_t len = 0; |
| 454 | |
| 455 | history_dir = utils::get_models_directory() + path_sep + "history"; |
| 456 | |
| 457 | #else |
| 458 | const char* model_path_env = std::getenv("FLM_MODEL_PATH"); |
| 459 | if (model_path_env && *model_path_env) { |
| 460 | history_dir = std::string(model_path_env) + path_sep + "history"; |
| 461 | } else { |
| 462 | std::string documents_dir = utils::get_user_directory(); |
| 463 | history_dir = documents_dir + path_sep + "flm" + path_sep + "history"; |
| 464 | } |
| 465 | #endif |
| 466 | |
| 467 | // Create the history directory if it doesn't exist |
| 468 | if (!std::filesystem::exists(history_dir)) { |
| 469 | std::filesystem::create_directories(history_dir); |
| 470 | } |
| 471 | |
| 472 | // save file to history_hh_mm_mm_dd_yyyy.txt |
| 473 | // 1) get current date |
| 474 | std::time_t t = std::time(nullptr); |
| 475 | std::tm tm = *std::localtime(&t); |
| 476 | |
| 477 | std::ostringstream date_ss; |
| 478 | date_ss << std::setw(2) << std::setfill('0') << tm.tm_hour |
| 479 | << '_' |
| 480 | << std::setw(2) << std::setfill('0') << tm.tm_min |
| 481 | << '_' |
| 482 | << std::setw(2) << std::setfill('0') << tm.tm_mon + 1 |
| 483 | << '_' |
| 484 | << std::setw(2) << std::setfill('0') << tm.tm_mday |
| 485 | << '_' |
| 486 | << (tm.tm_year + 1900); |
| 487 | std::string date_str = date_ss.str(); |
| 488 | |
| 489 | // 2) build filename in the history directory |
| 490 | std::string file_name = history_dir + path_sep + "history_" + date_str + ".txt"; |
| 491 | std::ofstream file(file_name); |
| 492 | if (file.is_open()) { |
| 493 | file << history.first << std::endl; |
| 494 | file.close(); |
| 495 | std::cout << "History saved to " << file_name << std::endl; |
| 496 | } |
| 497 | else { |
| 498 | std::cout << "Failed to open file: " << file_name << std::endl; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /// \brief Show the model information |
no test coverage detected