1#pragma once
 2
 3#include "common.h"
 4#include "arg.h"
 5
 6#include <string>
 7#include <vector>
 8#include <map>
 9#include <set>
10
11//
12// INI preset parser and writer
13//
14
15constexpr const char * COMMON_PRESET_DEFAULT_NAME = "default";
16
17struct common_preset_context;
18
19struct common_preset {
20    std::string name;
21
22    // options are stored as common_arg to string mapping, representing CLI arg and its value
23    std::map<common_arg, std::string> options;
24
25    // convert preset to CLI argument list
26    std::vector<std::string> to_args(const std::string & bin_path = "") const;
27
28    // convert preset to INI format string
29    std::string to_ini() const;
30
31    // TODO: maybe implement to_env() if needed
32
33    // modify preset options where argument is identified by its env variable
34    void set_option(const common_preset_context & ctx, const std::string & env, const std::string & value);
35
36    // unset option by its env variable
37    void unset_option(const std::string & env);
38
39    // get option value by its env variable, return false if not found
40    bool get_option(const std::string & env, std::string & value) const;
41
42    // merge another preset into this one, overwriting existing options
43    void merge(const common_preset & other);
44
45    // apply preset options to common_params
46    void apply_to_params(common_params & params) const;
47};
48
49// interface for multiple presets in one file
50using common_presets = std::map<std::string, common_preset>;
51
52// context for loading and editing presets
53struct common_preset_context {
54    common_params default_params; // unused for now
55    common_params_context ctx_params;
56    std::map<std::string, common_arg> key_to_opt;
57
58    bool filter_allowed_keys = false;
59    std::set<std::string> allowed_keys;
60
61    // if only_remote_allowed is true, only accept whitelisted keys
62    common_preset_context(llama_example ex, bool only_remote_allowed = false);
63
64    // load presets from INI file
65    common_presets load_from_ini(const std::string & path, common_preset & global) const;
66
67    // generate presets from cached models
68    common_presets load_from_cache() const;
69
70    // generate presets from local models directory
71    // for the directory structure, see "Using multiple models" in server/README.md
72    common_presets load_from_models_dir(const std::string & models_dir) const;
73
74    // generate one preset from CLI arguments
75    common_preset load_from_args(int argc, char ** argv) const;
76
77    // cascade multiple presets if exist on both: base < added
78    // if preset does not exist in base, it will be added without modification
79    common_presets cascade(const common_presets & base, const common_presets & added) const;
80
81    // apply presets over a base preset (same idea as CSS cascading)
82    common_presets cascade(const common_preset & base, const common_presets & presets) const;
83};