Class: Aidp::Harness::ProviderFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/harness/provider_factory.rb

Overview

Factory for creating configured provider instances

Uses AgentHarness providers for all provider instantiation.

Constant Summary collapse

PROVIDER_CLASSES =

For backwards compatibility - expose PROVIDER_CLASSES as a method

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_manager = nil) ⇒ ProviderFactory

Deprecated - use provider_classes method



28
29
30
31
32
# File 'lib/aidp/harness/provider_factory.rb', line 28

def initialize(config_manager = nil)
  @config_manager = config_manager || ConfigManager.new
  @provider_configs = {}
  @provider_instances = {}
end

Instance Attribute Details

#provider_configsObject (readonly)

Expose for testability



13
14
15
# File 'lib/aidp/harness/provider_factory.rb', line 13

def provider_configs
  @provider_configs
end

#provider_instancesObject (readonly)

Expose for testability



13
14
15
# File 'lib/aidp/harness/provider_factory.rb', line 13

def provider_instances
  @provider_instances
end

Class Method Details

.provider_classesObject

Provider class lookup - delegates to AgentHarness registry



16
17
18
19
20
21
22
23
# File 'lib/aidp/harness/provider_factory.rb', line 16

def self.provider_classes
  registry = AgentHarness::Providers::Registry.instance
  registry.all.each_with_object({}) do |name, hash|
    hash[name.to_s] = registry.get(name)
    # Add aliases
    hash["anthropic"] = registry.get(:claude) if name == :claude
  end
end

Instance Method Details

#all_provider_summaries(options = {}) ⇒ Object

Get all provider summaries



181
182
183
184
185
186
# File 'lib/aidp/harness/provider_factory.rb', line 181

def all_provider_summaries(options = {})
  configured_providers = configured_providers(options)
  configured_providers.map do |provider_name|
    provider_summary(provider_name, options)
  end
end

#clear_cacheObject

Clear provider cache



235
236
237
238
# File 'lib/aidp/harness/provider_factory.rb', line 235

def clear_cache
  @provider_instances.clear
  @provider_configs.clear
end

#configured_providers(options = {}) ⇒ Object

Get configured provider names



143
144
145
# File 'lib/aidp/harness/provider_factory.rb', line 143

def configured_providers(options = {})
  @config_manager.provider_names(options)
end

#create_all_providers(options = {}) ⇒ Object

Create all configured providers



80
81
82
83
# File 'lib/aidp/harness/provider_factory.rb', line 80

def create_all_providers(options = {})
  configured_providers = @config_manager.provider_names(options)
  create_providers(configured_providers, options)
end

#create_provider(provider_name, options = {}) ⇒ Object

Create a provider instance with configuration



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/aidp/harness/provider_factory.rb', line 35

def create_provider(provider_name, options = {})
  provider_name = provider_name.to_s
  cache_key = "#{provider_name}_#{options.hash}"

  # Return cached instance if available
  if @provider_instances[cache_key] && !options[:force_reload]
    return @provider_instances[cache_key]
  end

  # Get provider configuration
  provider_config = provider_config(provider_name)

  # Check if provider is configured and enabled
  unless provider_config.configured?(options)
    raise "Provider '#{provider_name}' is not configured"
  end

  unless provider_config.enabled?(options)
    raise "Provider '#{provider_name}' is disabled"
  end

  # Get provider class
  provider_class = provider_class(provider_name)
  raise "Unknown provider: #{provider_name}" unless provider_class

  # Create provider instance
  provider_instance = provider_class.new

  # Configure the provider instance
  configure_provider(provider_instance, provider_config, options)

  # Cache the instance
  @provider_instances[cache_key] = provider_instance

  provider_instance
end

#create_providers(provider_names, options = {}) ⇒ Object

Create multiple provider instances



73
74
75
76
77
# File 'lib/aidp/harness/provider_factory.rb', line 73

def create_providers(provider_names, options = {})
  provider_names.map do |provider_name|
    create_provider(provider_name, options)
  end
end

#create_providers_by_priority(options = {}) ⇒ Object

Create providers by priority



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/aidp/harness/provider_factory.rb', line 86

def create_providers_by_priority(options = {})
  all_providers = @config_manager.all_providers(options)

  # Sort by priority (lower number = higher priority)
  sorted_providers = all_providers.sort_by do |name, config|
    priority = config[:priority] || config["priority"] || 1
    [priority, name]
  end

  sorted_providers.map do |name, _config|
    create_provider(name, options)
  end
end

#create_providers_by_weight(options = {}) ⇒ Object

Create providers by weight (for load balancing)



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/aidp/harness/provider_factory.rb', line 101

def create_providers_by_weight(options = {})
  all_providers = @config_manager.all_providers(options)
  weighted_providers = []

  all_providers.each do |name, config|
    weight = config[:weight] || config["weight"] || 1
    weight.times { weighted_providers << name }
  end

  weighted_providers.shuffle.map do |name|
    create_provider(name, options)
  end
end

#enabled_providers(options = {}) ⇒ Object

Get enabled provider names



148
149
150
151
152
153
154
# File 'lib/aidp/harness/provider_factory.rb', line 148

def enabled_providers(options = {})
  configured_providers = configured_providers(options)
  configured_providers.select do |provider_name|
    provider_config = provider_config(provider_name)
    provider_config.enabled?(options)
  end
end

#provider_capabilities(provider_name, options = {}) ⇒ Object

Get provider capabilities



157
158
159
160
# File 'lib/aidp/harness/provider_factory.rb', line 157

def provider_capabilities(provider_name, options = {})
  provider_config = provider_config(provider_name)
  provider_config.capabilities(options)
end

#provider_class(provider_name) ⇒ Object

Get provider class from AgentHarness registry



121
122
123
124
125
126
127
128
# File 'lib/aidp/harness/provider_factory.rb', line 121

def provider_class(provider_name)
  name = provider_name.to_s
  # Handle anthropic -> claude alias
  name = "claude" if name == "anthropic"
  AgentHarness::Providers::Registry.instance.get(name.to_sym)
rescue AgentHarness::ConfigurationError
  nil
end

#provider_config(provider_name) ⇒ Object

Get provider configuration



116
117
118
# File 'lib/aidp/harness/provider_factory.rb', line 116

def provider_config(provider_name)
  @provider_configs[provider_name.to_s] ||= ProviderConfig.new(provider_name, @config_manager)
end

#provider_models(provider_name, options = {}) ⇒ Object

Get provider models



169
170
171
172
# File 'lib/aidp/harness/provider_factory.rb', line 169

def provider_models(provider_name, options = {})
  provider_config = provider_config(provider_name)
  provider_config.models(options)
end

#provider_summary(provider_name, options = {}) ⇒ Object

Get provider summary



175
176
177
178
# File 'lib/aidp/harness/provider_factory.rb', line 175

def provider_summary(provider_name, options = {})
  provider_config = provider_config(provider_name)
  provider_config.summary(options)
end

#provider_supported?(provider_name) ⇒ Boolean

Check if provider is supported

Returns:

  • (Boolean)


131
132
133
134
135
# File 'lib/aidp/harness/provider_factory.rb', line 131

def provider_supported?(provider_name)
  name = provider_name.to_s
  name = "claude" if name == "anthropic"
  AgentHarness::Providers::Registry.instance.registered?(name.to_sym)
end

#provider_supports_feature?(provider_name, feature, options = {}) ⇒ Boolean

Check if provider supports feature

Returns:

  • (Boolean)


163
164
165
166
# File 'lib/aidp/harness/provider_factory.rb', line 163

def provider_supports_feature?(provider_name, feature, options = {})
  provider_config = provider_config(provider_name)
  provider_config.supports_feature?(feature, options)
end

#reload_configObject

Reload configuration



241
242
243
244
# File 'lib/aidp/harness/provider_factory.rb', line 241

def reload_config
  clear_cache
  @config_manager.reload_config
end

#supported_providersObject

Get supported provider names



138
139
140
# File 'lib/aidp/harness/provider_factory.rb', line 138

def supported_providers
  AgentHarness::Providers::Registry.instance.all.map(&:to_s)
end

#validate_all_provider_configs(options = {}) ⇒ Object

Validate all provider configurations



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/aidp/harness/provider_factory.rb', line 222

def validate_all_provider_configs(options = {})
  configured_providers = configured_providers(options)
  all_errors = {}

  configured_providers.each do |provider_name|
    errors = validate_provider_config(provider_name, options)
    all_errors[provider_name] = errors unless errors.empty?
  end

  all_errors
end

#validate_provider_config(provider_name, options = {}) ⇒ Object

Validate provider configuration



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/aidp/harness/provider_factory.rb', line 189

def validate_provider_config(provider_name, options = {})
  provider_config = provider_config(provider_name)
  errors = []

  # Check if provider is configured
  unless provider_config.configured?(options)
    errors << "Provider '#{provider_name}' is not configured"
    return errors
  end

  # Check if provider is supported
  unless provider_supported?(provider_name)
    errors << "Provider '#{provider_name}' is not supported"
  end

  # Check required configuration
  if provider_config.usage_based_provider?(options)
    api_key = provider_config.api_key(options)
    unless api_key && !api_key.empty?
      errors << "API key not configured for provider '#{provider_name}'"
    end
  end

  # Check models configuration
  models = provider_config.models(options)
  if models.empty?
    errors << "No models configured for provider '#{provider_name}'"
  end

  errors
end