Class: AIA::Cli
- Inherits:
-
Object
- Object
- AIA::Cli
- Defined in:
- lib/aia/cli.rb
Constant Summary collapse
- CF_FORMATS =
%w[yml yaml toml]
- ENV_PREFIX =
self.name.split('::').first.upcase + "_"
- MAN_PAGE_PATH =
Pathname.new(__dir__) + '../../man/aia.1'
Instance Method Summary collapse
- #arguments ⇒ Object
- #check_for(option_sym) ⇒ Object
- #check_for_role_parameter ⇒ Object
- #convert_from_pathname_objects ⇒ Object
- #convert_pathname_objects!(converting_to_pathname: true) ⇒ Object
- #convert_to_pathname_objects ⇒ Object
- #dump_config_file ⇒ Object
- #error_on_invalid_option_combinations ⇒ Object
- #execute_immediate_commands ⇒ Object
-
#extract_extra_options ⇒ Object
Get the additional CLI arguments intended for the backend gen-AI processor.
-
#initialize(args) ⇒ Cli
constructor
A new instance of Cli.
- #invoke_fzf_to_choose_role ⇒ Object
- #load_config_file ⇒ Object
- #load_env_options ⇒ Object
- #parse_config_file ⇒ Object
- #pathname_to_string(pathname) ⇒ Object
- #prepare_config_as_hash ⇒ Object
- #process_command_line_arguments ⇒ Object
- #replace_erb_in_config_file ⇒ Object
- #setup_options_with_defaults(args) ⇒ Object
- #setup_prompt_manager ⇒ Object
- #show_completion ⇒ Object
- #show_error_usage ⇒ Object
-
#show_usage ⇒ Object
(also: #show_help)
aia usage is maintained in a man page.
- #show_verbose_usage ⇒ Object
- #show_version ⇒ Object
- #string_to_pathname(string) ⇒ Object
Constructor Details
#initialize(args) ⇒ Cli
Returns a new instance of Cli.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/aia/cli.rb', line 19 def initialize(args) args = args.split(' ') if args.is_a? String (args) # 1. defaults # 2. over-ride with envars process_command_line_arguments # 3. over-ride with command line options # 4. over-ride everything with config file load_config_file unless AIA.config.config_file.nil? convert_to_pathname_objects error_on_invalid_option_combinations setup_prompt_manager execute_immediate_commands end |
Instance Method Details
#arguments ⇒ Object
202 203 204 |
# File 'lib/aia/cli.rb', line 202 def arguments AIA.config.arguments end |
#check_for(option_sym) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/aia/cli.rb', line 278 def check_for(option_sym) # sometimes @options has stuff that is not a command line option return if @options[option_sym].nil? || @options[option_sym].size <= 1 boolean = option_sym.to_s.end_with?('?') switches = @options[option_sym][1].split switches.each do |switch| if arguments.include?(switch) index = arguments.index(switch) if boolean AIA.config[option_sym] = switch.include?('-no-') ? false : true arguments.slice!(index,1) else if switch.include?('-no-') AIA.config[option_sym] = switch.include?('out_file') ? STDOUT : nil arguments.slice!(index,1) else value = arguments[index + 1] if value.nil? || value.start_with?('-') abort "ERROR: #{option_sym} requires a parameter value" elsif "--pipeline" == switch prompt_sequence = value.split(',') AIA.config[option_sym] = prompt_sequence arguments.slice!(index,2) else AIA.config[option_sym] = value arguments.slice!(index,2) end end end break end end end |
#check_for_role_parameter ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/aia/cli.rb', line 317 def check_for_role_parameter role = AIA.config.role return if role.empty? role_path = string_to_pathname(AIA.config.roles_dir) + "#{role}.txt" unless role_path.exist? puts "Role prompt '#{role}' not found. Invoking fzf to choose a role..." invoke_fzf_to_choose_role end end |
#convert_from_pathname_objects ⇒ Object
95 96 97 |
# File 'lib/aia/cli.rb', line 95 def convert_from_pathname_objects convert_pathname_objects!(converting_to_pathname: false) end |
#convert_pathname_objects!(converting_to_pathname: true) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/aia/cli.rb', line 36 def convert_pathname_objects!(converting_to_pathname: true) path_keys = AIA.config.keys.grep(/_(dir|file)\z/) path_keys.each do |key| case AIA.config[key] when String AIA.config[key] = string_to_pathname(AIA.config[key]) when Pathname AIA.config[key] = pathname_to_string(AIA.config[key]) unless converting_to_pathname end end end |
#convert_to_pathname_objects ⇒ Object
90 91 92 |
# File 'lib/aia/cli.rb', line 90 def convert_to_pathname_objects convert_pathname_objects!(converting_to_pathname: true) end |
#dump_config_file ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/aia/cli.rb', line 215 def dump_config_file a_hash = prepare_config_as_hash dump_file = Pathname.new AIA.config.dump_file extname = dump_file.extname.to_s.downcase case extname when '.yml', '.yaml' dump_file.write YAML.dump(a_hash) when '.toml' dump_file.write TomlRB.dump(a_hash) else abort "Invalid config file format (#{extname}) request. Only #{CF_FORMATS.join(', ')} are supported." end exit end |
#error_on_invalid_option_combinations ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/aia/cli.rb', line 49 def error_on_invalid_option_combinations # --chat is intended as an interactive exchange if AIA.config.chat? unless AIA.config.next.empty? abort "ERROR: Cannot use --next with --chat" end unless STDOUT == AIA.config.out_file abort "ERROR: Cannot use --out_file with --chat" end unless AIA.config.pipeline.empty? abort "ERROR: Cannot use --pipeline with --chat" end end # --next says which prompt to process next # but --pipeline gives an entire sequence of prompts for processing unless AIA.config.next.empty? unless AIA.config.pipeline.empty? abort "ERROR: Cannot use --pipeline with --next" end end end |
#execute_immediate_commands ⇒ Object
207 208 209 210 211 212 |
# File 'lib/aia/cli.rb', line 207 def execute_immediate_commands show_usage if AIA.config.help? show_version if AIA.config.version? dump_config_file if AIA.config.dump_file show_completion if AIA.config.completion end |
#extract_extra_options ⇒ Object
Get the additional CLI arguments intended for the backend gen-AI processor.
433 434 435 436 437 438 439 |
# File 'lib/aia/cli.rb', line 433 def extra_index = arguments.index('--') if extra_index AIA.config.extra = arguments.slice!(extra_index..-1)[1..].join(' ') end end |
#invoke_fzf_to_choose_role ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/aia/cli.rb', line 330 def invoke_fzf_to_choose_role roles_path = string_to_pathname AIA.config.roles_dir available_roles = roles_path .children .select { |f| '.txt' == f.extname} .map{|role| role.basename.to_s.gsub('.txt','')} fzf = AIA::Fzf.new( list: available_roles, directory: roles_path, prompt: 'Select Role:', extension: '.txt' ) chosen_role = fzf.run if chosen_role.nil? abort("No role selected. Exiting...") else AIA.config.role = chosen_role puts "Role changed to '#{chosen_role}'." end end |
#load_config_file ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/aia/cli.rb', line 130 def load_config_file if AIA.config.config_file.to_s.end_with?(".erb") replace_erb_in_config_file end AIA.config.config_file = Pathname.new(AIA.config.config_file) if AIA.config.config_file.exist? AIA.config.merge! parse_config_file else abort "Config file does not exist: #{AIA.config.config_file}" end end |
#load_env_options ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/aia/cli.rb', line 100 def known_keys = @options.keys keys = ENV.keys .select{|k| k.start_with?(ENV_PREFIX)} .map{|k| k.gsub(ENV_PREFIX,'').downcase.to_sym} keys.each do |key| envar_key = ENV_PREFIX + key.to_s.upcase if known_keys.include?(key) AIA.config[key] = ENV[envar_key] elsif known_keys.include?("#{key}?".to_sym) key = "#{key}?".to_sym AIA.config[key] = %w[true t yes yea y 1].include?(ENV[envar_key].strip.downcase) ? true : false else # This is a new config key AIA.config[key] = ENV[envar_key] end end end |
#parse_config_file ⇒ Object
442 443 444 445 446 447 448 449 450 451 |
# File 'lib/aia/cli.rb', line 442 def parse_config_file case AIA.config.config_file.extname.downcase when '.yaml', '.yml' YAML.safe_load(AIA.config.config_file.read) when '.toml' TomlRB.parse(AIA.config.config_file.read) else abort "Unsupported config file type: #{AIA.config.config_file.extname}" end end |
#pathname_to_string(pathname) ⇒ Object
85 86 87 |
# File 'lib/aia/cli.rb', line 85 def pathname_to_string(pathname) pathname.to_s end |
#prepare_config_as_hash ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/aia/cli.rb', line 234 def prepare_config_as_hash convert_from_pathname_objects a_hash = AIA.config.to_h a_hash['dump'] = nil %w[ arguments config_file dump_file ].each do |unwanted_key| a_hash.delete(unwanted_key) end a_hash end |
#process_command_line_arguments ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/aia/cli.rb', line 248 def process_command_line_arguments # get the options meant for the backend AI command # doing this first in case there are any options that conflict # between frontend and backend. @options.keys.each do |option| check_for option end = arguments.select{|a| a.start_with?('-')} unless .empty? puts <<~EOS ERROR: Unknown options: #{.join(' ')} EOS show_error_usage exit end # After all other arguments # are processed, check for role parameter. check_for_role_parameter end |
#replace_erb_in_config_file ⇒ Object
122 123 124 125 126 127 |
# File 'lib/aia/cli.rb', line 122 def replace_erb_in_config_file content = Pathname.new(AIA.config.config_file).read content = ERB.new(content).result(binding) AIA.config.config_file = AIA.config.config_file.to_s.gsub('.erb', '') Pathname.new(AIA.config.config_file).write content end |
#setup_options_with_defaults(args) ⇒ Object
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 |
# File 'lib/aia/cli.rb', line 144 def (args) # TODO: This structure if flat; consider making it # at least two levels totake advantage of # YAML and TOML capabilities to isolate # common options within a section. # @options = { # Default # Key Value, switches arguments: [args], # NOTE: after process, prompt_id and context_files will be left directives: [[]], # an empty Array as the default value extra: [''], # # model: ["gpt-4-1106-preview", "--llm --model"], speech_model: ["tts-1", "--sm --speech_model"], voice: ["alloy", "--voice"], # transcription_model: ["wisper-1", "--tm --transcription_model"], # dump_file: [nil, "--dump"], completion: [nil, "--completion"], # chat?: [false, "--chat"], debug?: [false, "-d --debug"], edit?: [false, "-e --edit"], erb?: [false, "--erb"], fuzzy?: [false, "-f --fuzzy"], help?: [false, "-h --help"], markdown?: [true, "-m --markdown --no-markdown --md --no-md"], render?: [false, "--render"], shell?: [false, "--shell"], speak?: [false, "--speak"], terse?: [false, "--terse"], verbose?: [false, "-v --verbose"], version?: [false, "--version"], # next: ['', "-n --next"], pipeline: [[], "--pipeline"], role: ['', "-r --role"], # config_file:[nil, "-c --config_file"], prompts_dir:["~/.prompts", "-p --prompts_dir"], roles_dir: ["~/.prompts/roles", "--roles_dir"], out_file: [STDOUT, "-o --out_file --no-out_file"], log_file: ["~/.prompts/_prompts.log", "-l --log_file --no-log_file"], # backend: ['mods', "-b --be --backend --no-backend"], # # text2image related ... # image_size: ['', '--is --image_size'], image_quality: ['', '--iq --image_quality'], } AIA.config = AIA::Config.new(@options.transform_values { |values| values.first }) end |
#setup_prompt_manager ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/aia/cli.rb', line 417 def setup_prompt_manager @prompt = nil PromptManager::Prompt.storage_adapter = PromptManager::Storage::FileSystemAdapter.config do |config| config.prompts_dir = AIA.config.prompts_dir config.prompt_extension = '.txt' config.params_extension = '.json' config.search_proc = nil # TODO: add the rgfzf script for search_proc end.new end |
#show_completion ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/aia/cli.rb', line 391 def show_completion shell = AIA.config.completion script = Pathname.new(__dir__) + "aia_completion.#{shell}" if script.exist? puts puts script.read puts else STDERR.puts <<~EOS ERROR: The shell '#{shell}' is not supported. EOS end exit end |
#show_error_usage ⇒ Object
356 357 358 359 360 361 362 363 |
# File 'lib/aia/cli.rb', line 356 def show_error_usage puts <<~ERROR_USAGE Usage: aia [options] PROMPT_ID [CONTEXT_FILE(s)] [-- EXTERNAL_OPTIONS]" Try 'aia --help' for more information." ERROR_USAGE end |
#show_usage ⇒ Object Also known as: show_help
aia usage is maintained in a man page
367 368 369 370 371 372 |
# File 'lib/aia/cli.rb', line 367 def show_usage @options[:help?][0] = false puts `man #{MAN_PAGE_PATH}` show_verbose_usage if AIA.config.verbose? exit end |
#show_verbose_usage ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/aia/cli.rb', line 376 def show_verbose_usage puts <<~EOS ====================================== == Currently selected Backend: #{AIA.config.backend} == ====================================== EOS puts `mods --help` if "mods" == AIA.config.backend puts `sgpt --help` if "sgpt" == AIA.config.backend puts end |
#show_version ⇒ Object
411 412 413 414 |
# File 'lib/aia/cli.rb', line 411 def show_version puts AIA::VERSION exit end |
#string_to_pathname(string) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/aia/cli.rb', line 72 def string_to_pathname(string) ['~/', '$HOME/'].each do |prefix| if string.start_with? prefix string = string.gsub(prefix, HOME.to_s+'/') break end end pathname = Pathname.new(string) pathname.relative? ? Pathname.pwd + pathname : pathname end |