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:, system_instruction: nil) ⇒ Object



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

def self.add_configuration(name:, api_key:, api_endpoint:, model_id:, provider:, system_instruction: nil)
  config_data = {
    'name' => name,
    'api_key' => api_key,
    'api_endpoint' => api_endpoint,
    'model_id' => model_id,
    'provider' => provider,
    'system_instruction' => system_instruction || default_system_instruction
  }

  # 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



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

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',
      'system_instruction' => default_system_instruction
    }
  else
    # Ensure provider and system_instruction are set for backward compatibility
    config ||= {}
    config['provider'] ||= 'openai'
    config['system_instruction'] ||= default_system_instruction
    config
  end
end

.default_configuration_idObject



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

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

.default_system_instructionObject



117
118
119
120
# File 'lib/askcii/models/config.rb', line 117

def self.default_system_instruction
  'You are a command line application. Provide concise, terminal-friendly responses. ' \
    'Assume technical proficiency. Minimize explanations unless requested.'
end

.delete_configuration(id) ⇒ Object



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

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



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

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



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

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