Module: AiCli::Helpers::Config
- Defined in:
- lib/aicli/helpers/config.rb
Constant Summary collapse
- HOME_DIR =
File.join(Dir.home, '.aicli')
- CONFIG_PATH =
File.join(HOME_DIR, 'config')
- LEGACY_FLAT_CONFIG_PATH =
File.join(Dir.home, '.aicli')
- LEGACY_CONFIG_PATH =
File.join(Dir.home, '.ai-shell')
- CONFIG_PARSERS =
{ 'PROVIDER' => lambda { |provider| Llm.normalize_provider(provider) }, 'OPENAI_KEY' => lambda { |key| key.to_s }, 'ANTHROPIC_KEY' => lambda { |key| key.to_s }, 'MODEL' => lambda { |model| model.to_s }, 'SILENT_MODE' => lambda { |mode| mode.to_s.downcase == 'true' }, 'OPENAI_API_ENDPOINT' => lambda { |api_endpoint| api_endpoint.nil? || api_endpoint.empty? ? 'https://api.openai.com/v1' : api_endpoint }, 'LANGUAGE' => lambda { |language| language.nil? || language.empty? ? 'en' : language } }.freeze
Class Method Summary collapse
- .display_hint(config, key) ⇒ Object
- .ensure_home! ⇒ Object
- .get(cli_config = nil) ⇒ Object
- .has_own?(object, key) ⇒ Boolean
- .home_dir ⇒ Object
- .migrate_flat_config! ⇒ Object
- .parse_ini(content) ⇒ Object
- .read_config_file ⇒ Object
- .set(key_values) ⇒ Object
- .show_config_ui ⇒ Object
- .stringify_ini(config) ⇒ Object
Class Method Details
.display_hint(config, key) ⇒ Object
239 240 241 242 243 244 245 246 |
# File 'lib/aicli/helpers/config.rb', line 239 def display_hint(config, key) if config.key?(key) && !config[key].nil? && !(config[key].respond_to?(:empty?) && config[key].empty?) value = config[key] block_given? ? yield(value) : value.to_s else I18n.t('(not set)') end end |
.ensure_home! ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/aicli/helpers/config.rb', line 46 def ensure_home! if File.file?(LEGACY_FLAT_CONFIG_PATH) && !File.directory?(LEGACY_FLAT_CONFIG_PATH) migrate_flat_config! else FileUtils.mkdir_p(HOME_DIR) end HOME_DIR end |
.get(cli_config = nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/aicli/helpers/config.rb', line 67 def get(cli_config = nil) config = read_config_file parsed = {} CONFIG_PARSERS.each do |key, parser| value = cli_config&.dig(key) || config[key] parsed[key] = parser.call(value) end raw_model = cli_config&.dig('MODEL') || config['MODEL'] if raw_model.nil? || raw_model.to_s.empty? parsed['MODEL'] = Llm.default_model_for(parsed['PROVIDER']) end provider, model = Llm.resolve_provider_and_model(parsed) parsed['PROVIDER'] = provider parsed['MODEL'] = model parsed end |
.has_own?(object, key) ⇒ Boolean
122 123 124 |
# File 'lib/aicli/helpers/config.rb', line 122 def has_own?(object, key) object.key?(key) end |
.home_dir ⇒ Object
41 42 43 44 |
# File 'lib/aicli/helpers/config.rb', line 41 def home_dir ensure_home! HOME_DIR end |
.migrate_flat_config! ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/aicli/helpers/config.rb', line 55 def migrate_flat_config! backup = "#{LEGACY_FLAT_CONFIG_PATH}.migrating.#{Process.pid}" File.rename(LEGACY_FLAT_CONFIG_PATH, backup) FileUtils.mkdir_p(HOME_DIR) FileUtils.mv(backup, CONFIG_PATH) rescue StandardError FileUtils.mkdir_p(HOME_DIR) if File.exist?(backup) FileUtils.mv(backup, CONFIG_PATH) unless File.exist?(CONFIG_PATH) end end |
.parse_ini(content) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/aicli/helpers/config.rb', line 222 def parse_ini(content) result = {} content.each_line do |line| line = line.strip next if line.empty? || line.start_with?('#', ';') next unless line.include?('=') key, value = line.split('=', 2) result[key.strip] = value.strip.gsub(/\A["']|["']\z/, '') end result end |
.read_config_file ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/aicli/helpers/config.rb', line 209 def read_config_file ensure_home! path = if File.file?(CONFIG_PATH) CONFIG_PATH elsif File.file?(LEGACY_CONFIG_PATH) LEGACY_CONFIG_PATH end return {} unless path parse_ini(File.read(path)) end |
.set(key_values) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/aicli/helpers/config.rb', line 87 def set(key_values) config = read_config_file updates = {} key_values.each do |key, value| unless CONFIG_PARSERS.key?(key) raise KnownError, "#{I18n.t('Invalid config property')}: #{key}" end updates[key] = CONFIG_PARSERS[key].call(value).to_s end # Keep PROVIDER and MODEL aligned with the RubyLLM registry. if updates.key?('MODEL') && !updates['MODEL'].empty? model_provider = Llm.provider_for_model(updates['MODEL']) if model_provider updates['PROVIDER'] = model_provider unless updates.key?('PROVIDER') end end if updates.key?('PROVIDER') provider = Llm.normalize_provider(updates['PROVIDER']) updates['PROVIDER'] = provider next_model = updates['MODEL'] || config['MODEL'] known_ids = Completion.get_models(provider).map { |m| m['id'] } if next_model.to_s.empty? || !known_ids.include?(next_model) updates['MODEL'] = Llm.default_model_for(provider) unless updates.key?('MODEL') && known_ids.include?(updates['MODEL']) end end updates.each { |key, value| config[key] = value } ensure_home! File.write(CONFIG_PATH, stringify_ini(config)) end |
.show_config_ui ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/aicli/helpers/config.rb', line 126 def show_config_ui pastel = Pastel.new prompt = TTY::Prompt.new(interrupt: :exit) loop do config = get choice = prompt.select("#{I18n.t('Set config')}:") do || .choice "#{I18n.t('Provider')} (#{display_hint(config, 'PROVIDER')})", 'PROVIDER' .choice "#{I18n.t('OpenAI Key')} (#{display_hint(config, 'OPENAI_KEY') { |v| "sk-...#{v[-3..]}" }})", 'OPENAI_KEY' .choice "#{I18n.t('Anthropic Key')} (#{display_hint(config, 'ANTHROPIC_KEY') { |v| "...#{v[-3..]}" }})", 'ANTHROPIC_KEY' .choice "#{I18n.t('OpenAI API Endpoint')} (#{display_hint(config, 'OPENAI_API_ENDPOINT')})", 'OPENAI_API_ENDPOINT' .choice "#{I18n.t('Silent Mode')} (#{display_hint(config, 'SILENT_MODE')})", 'SILENT_MODE' .choice "#{I18n.t('Model')} (#{display_hint(config, 'MODEL')})", 'MODEL' .choice "#{I18n.t('Language')} (#{display_hint(config, 'LANGUAGE')})", 'LANGUAGE' .choice I18n.t('Cancel'), 'cancel' end case choice when 'PROVIDER' provider = prompt.select(I18n.t('Pick a provider')) do || Llm::PROVIDERS.each { |p| .choice p, p } end updates = [['PROVIDER', provider]] current_model = get['MODEL'] known_ids = Completion.get_models(provider).map { |m| m['id'] } unless known_ids.include?(current_model) updates << ['MODEL', Llm.default_model_for(provider)] end set(updates) when 'OPENAI_KEY' key = prompt.ask(I18n.t('Enter your OpenAI API key')) do |q| q.required true end set([['OPENAI_KEY', key]]) when 'ANTHROPIC_KEY' key = prompt.ask(I18n.t('Enter your Anthropic API key')) do |q| q.required true end set([['ANTHROPIC_KEY', key]]) when 'OPENAI_API_ENDPOINT' api_endpoint = prompt.ask(I18n.t('Enter your OpenAI API Endpoint')) set([['OPENAI_API_ENDPOINT', api_endpoint]]) if api_endpoint when 'SILENT_MODE' silent = prompt.yes?(I18n.t('Enable silent mode?')) set([['SILENT_MODE', silent ? 'true' : 'false']]) when 'MODEL' cfg = get models = Completion.get_models(cfg['PROVIDER']) if models.empty? puts pastel.yellow(I18n.t('No models found for this provider.')) next end model = prompt.select(I18n.t('Pick a model.')) do || .default cfg['MODEL'] if models.any? { |m| m['id'] == cfg['MODEL'] } models.each do |m| label = m['name'].empty? || m['name'] == m['id'] ? m['id'] : "#{m['name']} (#{m['id']})" .choice label, m['id'] end end set([['MODEL', model]]) when 'LANGUAGE' language = prompt.select(I18n.t('Enter the language you want to use')) do || I18n.languages.each { |k, v| .choice v, k } end set([['LANGUAGE', language]]) I18n.set_language(language) when 'cancel' break end end rescue KnownError, StandardError => e puts "\n#{pastel.red('✖')} #{e.}" Error.handle_cli_error(e) exit 1 end |
.stringify_ini(config) ⇒ Object
235 236 237 |
# File 'lib/aicli/helpers/config.rb', line 235 def stringify_ini(config) "#{config.map { |k, v| "#{k}=#{v}" }.join("\n")}\n" end |