Class: Crimson::Config

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • provider (String, nil) (defaults to: nil)
  • model (String, nil) (defaults to: nil)
  • api_key (String, nil) (defaults to: nil)
  • base_url (String, nil) (defaults to: nil)
  • max_tokens (Integer) (defaults to: 8192)
  • thinking_level (String, nil) (defaults to: nil)


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_keyString, ... (readonly)

Returns:

  • (String, nil)

    provider name

  • (String, nil)

    model identifier

  • (String, nil)

    API key

  • (String, nil)

    custom base URL

  • (Integer)

    max tokens for responses

  • (String, nil)

    thinking level (off/low/medium/high)



15
16
17
# File 'lib/crimson/config.rb', line 15

def api_key
  @api_key
end

#base_urlString, ... (readonly)

Returns:

  • (String, nil)

    provider name

  • (String, nil)

    model identifier

  • (String, nil)

    API key

  • (String, nil)

    custom base URL

  • (Integer)

    max tokens for responses

  • (String, nil)

    thinking level (off/low/medium/high)



15
16
17
# File 'lib/crimson/config.rb', line 15

def base_url
  @base_url
end

#max_tokensString, ... (readonly)

Returns:

  • (String, nil)

    provider name

  • (String, nil)

    model identifier

  • (String, nil)

    API key

  • (String, nil)

    custom base URL

  • (Integer)

    max tokens for responses

  • (String, nil)

    thinking level (off/low/medium/high)



15
16
17
# File 'lib/crimson/config.rb', line 15

def max_tokens
  @max_tokens
end

#modelString, ... (readonly)

Returns:

  • (String, nil)

    provider name

  • (String, nil)

    model identifier

  • (String, nil)

    API key

  • (String, nil)

    custom base URL

  • (Integer)

    max tokens for responses

  • (String, nil)

    thinking level (off/low/medium/high)



15
16
17
# File 'lib/crimson/config.rb', line 15

def model
  @model
end

#providerString, ... (readonly)

Returns:

  • (String, nil)

    provider name

  • (String, nil)

    model identifier

  • (String, nil)

    API key

  • (String, nil)

    custom base URL

  • (Integer)

    max tokens for responses

  • (String, nil)

    thinking level (off/low/medium/high)



15
16
17
# File 'lib/crimson/config.rb', line 15

def provider
  @provider
end

#thinking_levelString, ...

Returns:

  • (String, nil)

    provider name

  • (String, nil)

    model identifier

  • (String, nil)

    API key

  • (String, nil)

    custom base URL

  • (Integer)

    max tokens for responses

  • (String, nil)

    thinking level (off/low/medium/high)



15
16
17
# File 'lib/crimson/config.rb', line 15

def thinking_level
  @thinking_level
end

Class Method Details

.loadConfig

Load configuration from the JSON config file.

Returns:



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.message}"
end

Instance Method Details

#savevoid

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.

Returns:

  • (Boolean)

    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