Class: RubyCoded::Auth::AuthManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_coded/auth/auth_manager.rb

Overview

This class is used to manage the authentication process for the different AI providers

Constant Summary collapse

PROVIDERS =
{
  openai: Providers::OpenAI,
  anthropic: Providers::Anthropic
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config_path: UserConfig::CONFIG_PATH) ⇒ AuthManager

Returns a new instance of AuthManager.



24
25
26
# File 'lib/ruby_coded/auth/auth_manager.rb', line 24

def initialize(config_path: UserConfig::CONFIG_PATH)
  @config_path = config_path
end

Instance Method Details

#authenticated_provider_namesObject



47
48
49
# File 'lib/ruby_coded/auth/auth_manager.rb', line 47

def authenticated_provider_names
  PROVIDERS.keys.select { |name| credential_store.retrieve(name) }
end

#check_authenticationObject



51
52
53
54
55
56
# File 'lib/ruby_coded/auth/auth_manager.rb', line 51

def check_authentication
  return if configured_providers.any? { |name| credential_store.retrieve(name) }

  provider_name = choose_provider
  (provider_name)
end

#configure_ruby_llm!Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_coded/auth/auth_manager.rb', line 63

def configure_ruby_llm!
  RubyLLM.configure do |config|
    config.max_retries = 1

    PROVIDERS.each do |name, provider|
      credentials = credential_store.retrieve(name)
      next unless credentials

      credentials = refresh_if_expired(name, provider, credentials)
      key = extract_api_key(credentials)
      config.send("#{provider.ruby_llm_key}=", key)
    end
  end
end

#configured_providersObject



43
44
45
# File 'lib/ruby_coded/auth/auth_manager.rb', line 43

def configured_providers
  PROVIDERS.keys
end

#login(provider_name) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/ruby_coded/auth/auth_manager.rb', line 28

def (provider_name)
  provider = PROVIDERS.fetch(provider_name)
  strategy = strategy_for(provider)
  credentials = strategy.authenticate
  credential_store.store(provider_name, credentials)
  configure_ruby_llm!
  print_api_credits_notice(provider)
  credentials
end

#login_promptObject



58
59
60
61
# File 'lib/ruby_coded/auth/auth_manager.rb', line 58

def 
  provider_name = choose_provider
  (provider_name)
end

#logout(provider_name) ⇒ Object



38
39
40
41
# File 'lib/ruby_coded/auth/auth_manager.rb', line 38

def logout(provider_name)
  credential_store.remove(provider_name)
  configure_ruby_llm!
end