Class: Askcii::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/askcii/models/config.rb

Class Method Summary collapse

Class Method Details

.add_configuration(name, api_key, api_endpoint, model_id, provider) ⇒ Object



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

def self.add_configuration(name, api_key, api_endpoint, model_id, provider)
  config_data = {
    'name' => name,
    'api_key' => api_key,
    'api_endpoint' => api_endpoint,
    'model_id' => model_id,
    'provider' => provider
  }

  # Find the next available ID
  existing_ids = configurations.map { |c| c['id'].to_i }.sort
  next_id = existing_ids.empty? ? 1 : existing_ids.last + 1

  set("config_#{next_id}", config_data.to_json)
end

.api_endpointObject



22
23
24
# File 'lib/askcii/models/config.rb', line 22

def self.api_endpoint
  get('api_endpoint')
end

.api_keyObject

Legacy methods for backward compatibility



18
19
20
# File 'lib/askcii/models/config.rb', line 18

def self.api_key
  get('api_key')
end

.configurationsObject

New multi-configuration methods



31
32
33
34
35
36
37
38
# File 'lib/askcii/models/config.rb', line 31

def self.configurations
  where(Sequel.like(:key, 'config_%')).map do |config|
    config_data = JSON.parse(config.value)
    config_data.merge('id' => config.key.split('_', 2)[1])
  end
rescue JSON::ParserError
  []
end

.current_configurationObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/askcii/models/config.rb', line 94

def self.current_configuration
  default_id = default_configuration_id
  config = get_configuration(default_id)

  # Fallback to legacy configuration if no multi-configs exist
  if config.nil? && configurations.empty?
    {
      'api_key' => api_key,
      'api_endpoint' => api_endpoint,
      'model_id' => model_id,
      'provider' => 'openai'
    }
  else
    # Ensure provider is set for backward compatibility
    config ||= {}
    config['provider'] ||= 'openai'
    config
  end
end

.default_configuration_idObject



65
66
67
# File 'lib/askcii/models/config.rb', line 65

def self.default_configuration_id
  get('default_config_id') || '1'
end

.delete_configuration(id) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/askcii/models/config.rb', line 73

def self.delete_configuration(id)
  config = find(key: "config_#{id}")
  return false unless config

  # Check if this is the default configuration
  if default_configuration_id == id.to_s
    # Reset default to the first remaining configuration
    remaining_configs = configurations.reject { |c| c['id'] == id.to_s }
    if remaining_configs.any?
      set_default_configuration(remaining_configs.first['id'])
    else
      # If no configurations remain, clear the default
      config_record = find(key: 'default_config_id')
      config_record&.delete
    end
  end

  config.delete
  true
end

.get(key) ⇒ Object



12
13
14
15
# File 'lib/askcii/models/config.rb', line 12

def self.get(key)
  config = find(key: key)
  config&.value
end

.get_configuration(id) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/askcii/models/config.rb', line 56

def self.get_configuration(id)
  config = get("config_#{id}")
  return nil unless config

  JSON.parse(config)
rescue JSON::ParserError
  nil
end

.model_idObject



26
27
28
# File 'lib/askcii/models/config.rb', line 26

def self.model_id
  get('model_id')
end

.set(key, value) ⇒ Object



7
8
9
10
# File 'lib/askcii/models/config.rb', line 7

def self.set(key, value)
  config = find_or_create(key: key)
  config.update(value: value)
end

.set_default_configuration(id) ⇒ Object



69
70
71
# File 'lib/askcii/models/config.rb', line 69

def self.set_default_configuration(id)
  set('default_config_id', id.to_s)
end