Class: Ace::LLM::Molecules::ProviderLoader
- Inherits:
-
Object
- Object
- Ace::LLM::Molecules::ProviderLoader
- Defined in:
- lib/ace/llm/molecules/provider_loader.rb
Overview
ProviderLoader handles dynamic loading of provider gems and classes
Instance Attribute Summary collapse
-
#load_errors ⇒ Object
readonly
Returns the value of attribute load_errors.
-
#loaded_gems ⇒ Object
readonly
Returns the value of attribute loaded_gems.
Instance Method Summary collapse
-
#available_provider_gems ⇒ Array<String>
Get list of available provider gems.
-
#clear_cache ⇒ Object
Clear loaded gems cache (useful for testing).
-
#gem_available?(gem_name) ⇒ Boolean
Check if a gem is available (without loading it).
-
#initialize ⇒ ProviderLoader
constructor
A new instance of ProviderLoader.
-
#load_class(class_name, gem_name: nil) ⇒ Class
Load a provider class.
-
#load_gem(gem_name, required: true) ⇒ Boolean
Load a provider gem.
-
#try_load_provider(provider_config) ⇒ Hash
Try to load a provider and return status.
Constructor Details
#initialize ⇒ ProviderLoader
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_errors ⇒ Object (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_gems ⇒ Object (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_gems ⇒ Array<String>
Get list of available 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_cache ⇒ Object
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)
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
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
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. if required raise LoadError, "Required provider gem '#{gem_name}' could not be loaded: #{e.}" else false end end end |
#try_load_provider(provider_config) ⇒ Hash
Try to load a provider and return status
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.}" rescue => e status[:error] = "Error loading provider: #{e.}" end status end |