Module: AIA::ConfigModules::FileLoader

Defined in:
lib/aia/config/file_loader.rb

Class Method Summary collapse

Class Method Details

.apply_file_config_to_struct(config, file_config) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/aia/config/file_loader.rb', line 80

def apply_file_config_to_struct(config, file_config)
  file_config.each do |key, value|
    # Special handling for model array with roles (ADR-005 v2)
    if (key == :model || key == 'model') && value.is_a?(Array) && value.first.is_a?(Hash)
      config[:model] = process_model_array_with_roles(value)
    else
      config[key] = value
    end
  end
end

.cf_options(file) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aia/config/file_loader.rb', line 41

def cf_options(file)
  config = OpenStruct.new

  if File.exist?(file)
    content = read_and_process_config_file(file)
    file_config = parse_config_content(content, File.extname(file).downcase)
    apply_file_config_to_struct(config, file_config)
  else
    STDERR.puts "WARNING:Config file not found: #{file}"
  end

  normalize_last_refresh_date(config)
  config
end

.dump_config(config, file) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/aia/config/file_loader.rb', line 124

def dump_config(config, file)
  # Implementation for config dump
  ext = File.extname(file).downcase

  config.last_refresh = config.last_refresh.to_s if config.last_refresh.is_a? Date

  config_hash = config.to_h

  # Remove prompt_id to prevent automatic initial pompting in --chat mode
  config_hash.delete(:prompt_id)

  # Remove dump_file key to prevent automatic exit on next load
  config_hash.delete(:dump_file)

  content = case ext
            when '.yml', '.yaml'
              YAML.dump(config_hash)
            when '.toml'
              TomlRB.dump(config_hash)
            else
              raise "Unsupported config file format: #{ext}"
            end

  File.write(file, content)
  puts "Config successfully dumped to #{file}"
end

.generate_completion_script(shell) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/aia/config/file_loader.rb', line 151

def generate_completion_script(shell)
  script_path = File.join(File.dirname(__FILE__), "../../aia_completion.#{shell}")

  if File.exist?(script_path)
    puts File.read(script_path)
  else
    STDERR.puts "ERROR: The shell '#{shell}' is not supported or the completion script is missing."
  end
end

.load_config_file(file, config) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/aia/config/file_loader.rb', line 12

def load_config_file(file, config)
  if File.exist?(file)
    ext = File.extname(file).downcase
    content = File.read(file)

    # Process ERB if filename ends with .erb
    if file.end_with?('.erb')
      content = ERB.new(content).result
      file = file.chomp('.erb')
      File.write(file, content)
    end

    file_config = case ext
                  when '.yml', '.yaml'
                    YAML.safe_load(content, permitted_classes: [Symbol], symbolize_names: true)
                  when '.toml'
                    TomlRB.parse(content)
                  else
                    raise "Unsupported config file format: #{ext}"
                  end

    file_config.each do |key, value|
      config[key.to_sym] = value
    end
  else
    raise "Config file not found: #{file}"
  end
end

.normalize_last_refresh_date(config) ⇒ Object



118
119
120
121
122
# File 'lib/aia/config/file_loader.rb', line 118

def normalize_last_refresh_date(config)
  return unless config.last_refresh&.is_a?(String)

  config.last_refresh = Date.strptime(config.last_refresh, '%Y-%m-%d')
end

.parse_config_content(content, ext) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/aia/config/file_loader.rb', line 69

def parse_config_content(content, ext)
  case ext
  when '.yml', '.yaml'
    YAML.safe_load(content, permitted_classes: [Symbol], symbolize_names: true)
  when '.toml'
    TomlRB.parse(content)
  else
    raise "Unsupported config file format: #{ext}"
  end
end

.process_model_array_with_roles(models_array) ⇒ Object

Process model array with roles from config file (ADR-005 v2) Format: [“gpt-4o”, role: “architect”, …] Also supports models without roles: [“gpt-4o”, …]



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/aia/config/file_loader.rb', line 94

def process_model_array_with_roles(models_array)
  return [] if models_array.nil? || models_array.empty?

  model_specs = []
  model_counts = Hash.new(0)

  models_array.each do |spec|
    model_name = spec[:model] || spec['model']
    role_name = spec[:role] || spec['role']

    model_counts[model_name] += 1
    instance = model_counts[model_name]

    model_specs << {
      model: model_name,
      role: role_name,
      instance: instance,
      internal_id: instance > 1 ? "#{model_name}##{instance}" : model_name
    }
  end

  model_specs
end

.read_and_process_config_file(file) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aia/config/file_loader.rb', line 56

def read_and_process_config_file(file)
  content = File.read(file)

  # Process ERB if filename ends with .erb
  if file.end_with?('.erb')
    content = ERB.new(content).result
    processed_file = file.chomp('.erb')
    File.write(processed_file, content)
  end

  content
end