Class: Aidp::Harness::ProviderFactory

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

Overview

Factory for creating configured provider instances

Constant Summary collapse

PROVIDER_CLASSES =
{
  "cursor" => Aidp::Providers::Cursor,
  "anthropic" => Aidp::Providers::Anthropic,
  "claude" => Aidp::Providers::Anthropic,
  "gemini" => Aidp::Providers::Gemini,
  "opencode" => Aidp::Providers::Opencode,
  "kilocode" => Aidp::Providers::Kilocode,
  "github_copilot" => Aidp::Providers::GithubCopilot,
  "codex" => Aidp::Providers::Codex,
  "aider" => Aidp::Providers::Aider
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_manager = nil) ⇒ ProviderFactory

Returns a new instance of ProviderFactory.



33
34
35
36
37
# File 'lib/aidp/harness/provider_factory.rb', line 33

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



19
20
21
# File 'lib/aidp/harness/provider_factory.rb', line 19

def provider_configs
  @provider_configs
end

#provider_instancesObject (readonly)

Expose for testability



19
20
21
# File 'lib/aidp/harness/provider_factory.rb', line 19

def provider_instances
  @provider_instances
end

Instance Method Details

#all_provider_summaries(options = {}) ⇒ Object

Get all provider summaries



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

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



233
234
235
236
# File 'lib/aidp/harness/provider_factory.rb', line 233

def clear_cache
  @provider_instances.clear
  @provider_configs.clear
end

#configured_providers(options = {}) ⇒ Object

Get configured provider names



141
142
143
# File 'lib/aidp/harness/provider_factory.rb', line 141

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

#create_all_providers(options = {}) ⇒ Object

Create all configured providers



85
86
87
88
# File 'lib/aidp/harness/provider_factory.rb', line 85

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



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
71
72
73
74
75
# File 'lib/aidp/harness/provider_factory.rb', line 40

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



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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/aidp/harness/provider_factory.rb', line 91

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)



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/aidp/harness/provider_factory.rb', line 106

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



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

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



155
156
157
158
# File 'lib/aidp/harness/provider_factory.rb', line 155

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



126
127
128
# File 'lib/aidp/harness/provider_factory.rb', line 126

def provider_class(provider_name)
  PROVIDER_CLASSES[provider_name.to_s]
end

#provider_config(provider_name) ⇒ Object

Get provider configuration



121
122
123
# File 'lib/aidp/harness/provider_factory.rb', line 121

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



167
168
169
170
# File 'lib/aidp/harness/provider_factory.rb', line 167

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



173
174
175
176
# File 'lib/aidp/harness/provider_factory.rb', line 173

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
# File 'lib/aidp/harness/provider_factory.rb', line 131

def provider_supported?(provider_name)
  PROVIDER_CLASSES.key?(provider_name.to_s)
end

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

Check if provider supports feature

Returns:

  • (Boolean)


161
162
163
164
# File 'lib/aidp/harness/provider_factory.rb', line 161

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



239
240
241
242
# File 'lib/aidp/harness/provider_factory.rb', line 239

def reload_config
  clear_cache
  @config_manager.reload_config
end

#supported_providersObject

Get supported provider names



136
137
138
# File 'lib/aidp/harness/provider_factory.rb', line 136

def supported_providers
  PROVIDER_CLASSES.keys
end

#validate_all_provider_configs(options = {}) ⇒ Object

Validate all provider configurations



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

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



187
188
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
# File 'lib/aidp/harness/provider_factory.rb', line 187

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