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 Method Summary collapse

Constructor Details

#initialize(config_manager = nil) ⇒ ProviderFactory

Returns a new instance of ProviderFactory.



30
31
32
33
34
# File 'lib/aidp/harness/provider_factory.rb', line 30

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

Instance Method Details

#all_provider_summaries(options = {}) ⇒ Object

Get all provider summaries



176
177
178
179
180
181
# File 'lib/aidp/harness/provider_factory.rb', line 176

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



230
231
232
233
# File 'lib/aidp/harness/provider_factory.rb', line 230

def clear_cache
  @provider_instances.clear
  @provider_configs.clear
end

#configured_providers(options = {}) ⇒ Object

Get configured provider names



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

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

#create_all_providers(options = {}) ⇒ Object

Create all configured providers



82
83
84
85
# File 'lib/aidp/harness/provider_factory.rb', line 82

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



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

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



75
76
77
78
79
# File 'lib/aidp/harness/provider_factory.rb', line 75

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



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

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)



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

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



143
144
145
146
147
148
149
# File 'lib/aidp/harness/provider_factory.rb', line 143

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



152
153
154
155
# File 'lib/aidp/harness/provider_factory.rb', line 152

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



123
124
125
# File 'lib/aidp/harness/provider_factory.rb', line 123

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

#provider_config(provider_name) ⇒ Object

Get provider configuration



118
119
120
# File 'lib/aidp/harness/provider_factory.rb', line 118

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



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

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



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

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)


128
129
130
# File 'lib/aidp/harness/provider_factory.rb', line 128

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)


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

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



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

def reload_config
  clear_cache
  @config_manager.reload_config
end

#supported_providersObject

Get supported provider names



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

def supported_providers
  PROVIDER_CLASSES.keys
end

#validate_all_provider_configs(options = {}) ⇒ Object

Validate all provider configurations



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/aidp/harness/provider_factory.rb', line 217

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



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

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