Class: Ace::LLM::Molecules::ProviderLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/llm/molecules/provider_loader.rb

Overview

ProviderLoader handles dynamic loading of provider gems and classes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProviderLoader

Returns a new instance of ProviderLoader.



10
11
12
13
# File 'lib/ace/llm/molecules/provider_loader.rb', line 10

def initialize
  @loaded_gems = {}
  @load_errors = {}
end

Instance Attribute Details

#load_errorsObject (readonly)

Returns the value of attribute load_errors.



8
9
10
# File 'lib/ace/llm/molecules/provider_loader.rb', line 8

def load_errors
  @load_errors
end

#loaded_gemsObject (readonly)

Returns the value of attribute loaded_gems.



8
9
10
# File 'lib/ace/llm/molecules/provider_loader.rb', line 8

def loaded_gems
  @loaded_gems
end

Instance Method Details

#available_provider_gemsArray<String>

Get list of available provider gems

Returns:

  • (Array<String>)

    List of available ace-llm provider gems



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ace/llm/molecules/provider_loader.rb', line 110

def available_provider_gems
  gems = []

  # Check for known provider gems
  known_providers = %w[
    ace-llm-google
    ace-llm-openai
    ace-llm-anthropic
    ace-llm-mistral
    ace-llm-togetherai
  ]

  known_providers.each do |gem_name|
    gems << gem_name if gem_available?(gem_name)
  end

  gems
end

#clear_cacheObject

Clear loaded gems cache (useful for testing)



130
131
132
133
# File 'lib/ace/llm/molecules/provider_loader.rb', line 130

def clear_cache
  @loaded_gems.clear
  @load_errors.clear
end

#gem_available?(gem_name) ⇒ Boolean

Check if a gem is available (without loading it)

Parameters:

  • gem_name (String)

    Name of the gem

Returns:

  • (Boolean)

    True if gem is available



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ace/llm/molecules/provider_loader.rb', line 95

def gem_available?(gem_name)
  # Check if already loaded
  return true if @loaded_gems[gem_name]

  # Try to find the gem in the load path
  gem_path = gem_name.tr("-", "/")

  $LOAD_PATH.any? do |path|
    File.exist?(File.join(path, "#{gem_path}.rb")) ||
      File.directory?(File.join(path, gem_path))
  end
end

#load_class(class_name, gem_name: nil) ⇒ Class

Load a provider class

Parameters:

  • class_name (String)

    Full class name (e.g., “Ace::LLM::Organisms::GoogleClient”)

  • gem_name (String, nil) (defaults to: nil)

    Optional gem to load first

Returns:

  • (Class)

    The loaded class

Raises:

  • (NameError)

    If class cannot be resolved

  • (LoadError)

    If gem is required but cannot be loaded



45
46
47
48
49
50
51
# File 'lib/ace/llm/molecules/provider_loader.rb', line 45

def load_class(class_name, gem_name: nil)
  # Load gem if specified
  load_gem(gem_name, required: true) if gem_name

  # Resolve the class constant
  resolve_constant(class_name)
end

#load_gem(gem_name, required: true) ⇒ Boolean

Load a provider gem

Parameters:

  • gem_name (String)

    Name of the gem to load

  • required (Boolean) (defaults to: true)

    Whether the gem is required (raises if true and fails)

Returns:

  • (Boolean)

    True if loaded successfully

Raises:

  • (LoadError)

    If required is true and gem cannot be loaded



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ace/llm/molecules/provider_loader.rb', line 20

def load_gem(gem_name, required: true)
  return true if @loaded_gems[gem_name]

  begin
    # Try standard require first
    require_gem(gem_name)
    @loaded_gems[gem_name] = true
    true
  rescue LoadError => e
    @load_errors[gem_name] = e.message

    if required
      raise LoadError, "Required provider gem '#{gem_name}' could not be loaded: #{e.message}"
    else
      false
    end
  end
end

#try_load_provider(provider_config) ⇒ Hash

Try to load a provider and return status

Parameters:

  • provider_config (Hash)

    Provider configuration

Returns:

  • (Hash)

    Status information about the provider



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ace/llm/molecules/provider_loader.rb', line 56

def try_load_provider(provider_config)
  status = {
    name: provider_config["name"],
    gem: provider_config["gem"],
    class: provider_config["class"],
    loaded: false,
    available: false,
    error: nil
  }

  begin
    # Try to load gem if specified
    if provider_config["gem"]
      gem_loaded = load_gem(provider_config["gem"], required: false)
      status[:gem_loaded] = gem_loaded

      unless gem_loaded
        status[:error] = "Gem '#{provider_config["gem"]}' not available"
        return status
      end
    end

    # Try to resolve class
    klass = resolve_constant(provider_config["class"])
    status[:loaded] = true
    status[:available] = true
    status[:class_object] = klass
  rescue NameError => e
    status[:error] = "Class '#{provider_config["class"]}' not found: #{e.message}"
  rescue => e
    status[:error] = "Error loading provider: #{e.message}"
  end

  status
end