Class: Docit::Ai::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/docit/ai/configuration.rb

Constant Summary collapse

CONFIG_FILE =
".docit_ai.yml"
GENERATED_FILE_COMMENT =
<<~TEXT
  # If you want to change the model and start Docit setup again,
  # rerun: rails generate docit:install

TEXT
PROVIDERS =
%w[openai anthropic groq].freeze
DEFAULT_MODELS =
{
  "openai" => "gpt-4o-mini",
  "anthropic" => "claude-haiku-4-5-20251001",
  "groq" => "llama-3.3-70b-versatile"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider:, model:, api_key:) ⇒ Configuration

Returns a new instance of Configuration.



25
26
27
28
29
# File 'lib/docit/ai/configuration.rb', line 25

def initialize(provider:, model:, api_key:)
  @provider = provider.to_s
  @model = model.to_s
  @api_key = api_key.to_s
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



23
24
25
# File 'lib/docit/ai/configuration.rb', line 23

def api_key
  @api_key
end

#modelObject (readonly)

Returns the value of attribute model.



23
24
25
# File 'lib/docit/ai/configuration.rb', line 23

def model
  @model
end

#providerObject (readonly)

Returns the value of attribute provider.



23
24
25
# File 'lib/docit/ai/configuration.rb', line 23

def provider
  @provider
end

Class Method Details

.config_pathObject



36
37
38
# File 'lib/docit/ai/configuration.rb', line 36

def config_path
  Rails.root.join(CONFIG_FILE)
end

.configured?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/docit/ai/configuration.rb', line 40

def configured?
  File.exist?(config_path)
end

.loadObject

Raises:



44
45
46
47
48
49
50
51
52
53
# File 'lib/docit/ai/configuration.rb', line 44

def load
  raise Error, "AI not configured. Run: rails generate docit:ai_setup" if configured? == false

  data = YAML.safe_load_file(config_path, permitted_classes: [Symbol])
  new(
    provider: data["provider"],
    model: data["model"],
    api_key: data["api_key"]
  )
end

.save(provider:, model:, api_key:) ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/docit/ai/configuration.rb', line 55

def save(provider:, model:, api_key:)
  config = new(provider: provider, model: model, api_key: api_key)
  raise Error, "Invalid configuration" if config.valid? == false

  yaml = {
    "provider" => config.provider,
    "model" => config.model,
    "api_key" => config.api_key
  }.to_yaml

  File.write(config_path, GENERATED_FILE_COMMENT + yaml)
  File.chmod(0o600, config_path)

  config
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/docit/ai/configuration.rb', line 31

def valid?
  PROVIDERS.include?(provider) && !model.empty? && !api_key.empty?
end