Class: Crimson::Config
- Inherits:
-
Object
- Object
- Crimson::Config
- Defined in:
- lib/crimson/config.rb
Overview
Configuration model with JSON file persistence. Stores provider, model, API key, and other connection settings.
Constant Summary collapse
- VALID_THINKING_LEVELS =
Valid thinking level values.
%w[off low medium high].freeze
Instance Attribute Summary collapse
- #api_key ⇒ String, ... readonly
- #base_url ⇒ String, ... readonly
- #max_tokens ⇒ String, ... readonly
- #model ⇒ String, ... readonly
- #provider ⇒ String, ... readonly
- #thinking_level ⇒ String, ...
Class Method Summary collapse
-
.load ⇒ Config
Load configuration from the JSON config file.
Instance Method Summary collapse
-
#initialize(provider: nil, model: nil, api_key: nil, base_url: nil, max_tokens: 8192, thinking_level: nil) ⇒ Config
constructor
A new instance of Config.
-
#save ⇒ void
Persist configuration to the JSON config file with restricted permissions.
-
#valid? ⇒ Boolean
Whether required fields are present.
Constructor Details
#initialize(provider: nil, model: nil, api_key: nil, base_url: nil, max_tokens: 8192, thinking_level: nil) ⇒ Config
Returns a new instance of Config.
26 27 28 29 30 31 32 33 |
# File 'lib/crimson/config.rb', line 26 def initialize(provider: nil, model: nil, api_key: nil, base_url: nil, max_tokens: 8192, thinking_level: nil) @provider = provider @model = model @api_key = api_key @base_url = base_url @max_tokens = max_tokens @thinking_level = validate_thinking_level(thinking_level) end |
Instance Attribute Details
#api_key ⇒ String, ... (readonly)
15 16 17 |
# File 'lib/crimson/config.rb', line 15 def api_key @api_key end |
#base_url ⇒ String, ... (readonly)
15 16 17 |
# File 'lib/crimson/config.rb', line 15 def base_url @base_url end |
#max_tokens ⇒ String, ... (readonly)
15 16 17 |
# File 'lib/crimson/config.rb', line 15 def max_tokens @max_tokens end |
#model ⇒ String, ... (readonly)
15 16 17 |
# File 'lib/crimson/config.rb', line 15 def model @model end |
#provider ⇒ String, ... (readonly)
15 16 17 |
# File 'lib/crimson/config.rb', line 15 def provider @provider end |
#thinking_level ⇒ String, ...
15 16 17 |
# File 'lib/crimson/config.rb', line 15 def thinking_level @thinking_level end |
Class Method Details
.load ⇒ Config
Load configuration from the JSON config file.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/crimson/config.rb', line 37 def self.load return new unless File.exist?(Crimson::CONFIG_FILE) data = JSON.parse(File.read(Crimson::CONFIG_FILE)) new( provider: data["provider"], model: data["model"], api_key: data["api_key"], base_url: data["base_url"], max_tokens: data["max_tokens"] || 1000, thinking_level: data["thinking_level"] ) rescue JSON::ParserError => e raise Error, "Invalid config file: #{e.}" end |
Instance Method Details
#save ⇒ void
This method returns an undefined value.
Persist configuration to the JSON config file with restricted permissions.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/crimson/config.rb', line 55 def save FileUtils.mkdir_p(File.dirname(Crimson::CONFIG_FILE)) data = { provider: @provider, model: @model, api_key: @api_key, base_url: @base_url, max_tokens: @max_tokens, thinking_level: @thinking_level } File.write(Crimson::CONFIG_FILE, JSON.pretty_generate(data)) File.chmod(0o600, Crimson::CONFIG_FILE) end |
#valid? ⇒ Boolean
Returns whether required fields are present.
72 73 74 75 76 77 78 |
# File 'lib/crimson/config.rb', line 72 def valid? return false if @provider.nil? || @provider.empty? return false if @model.nil? || @model.empty? return false if @api_key.nil? || @api_key.empty? return false if @provider == "custom" && (@base_url.nil? || @base_url.empty?) true end |