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
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config_manager = nil) ⇒ ProviderFactory

Returns a new instance of ProviderFactory.



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 Method Details

#all_provider_summaries(options = {}) ⇒ Object

Get all provider summaries



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

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



228
229
230
231
# File 'lib/aidp/harness/provider_factory.rb', line 228

def clear_cache
  @provider_instances.clear
  @provider_configs.clear
end

#configured_providers(options = {}) ⇒ Object

Get configured provider names



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

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



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

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



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

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



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

def provider_class(provider_name)
  PROVIDER_CLASSES[provider_name.to_s]
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



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

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



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

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)


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

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)


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

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



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

def reload_config
  clear_cache
  @config_manager.reload_config
end

#supported_providersObject

Get supported provider names



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

def supported_providers
  PROVIDER_CLASSES.keys
end

#validate_all_provider_configs(options = {}) ⇒ Object

Validate all provider configurations



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

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



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

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