Class: AgentHarness::Providers::Base

Inherits:
Object
  • Object
show all
Includes:
Adapter
Defined in:
lib/agent_harness/providers/base.rb

Overview

Base class for all providers

Provides common functionality for provider implementations including command execution, error handling, and response parsing.

Examples:

Implementing a provider

class MyProvider < AgentHarness::Providers::Base
  class << self
    def provider_name
      :my_provider
    end

    def binary_name
      "my-cli"
    end

    def available?
      system("which my-cli > /dev/null 2>&1")
    end
  end

  protected

  def build_command(prompt, options)
    [self.class.binary_name, "--prompt", prompt]
  end
end

Direct Known Subclasses

Aider, Anthropic, Codex, Cursor, Gemini, GithubCopilot, Kilocode, Opencode

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Adapter

#capabilities, #dangerous_mode_flags, #error_patterns, #fetch_mcp_servers, #health_status, included, #session_flags, #supports_dangerous_mode?, #supports_mcp?, #supports_sessions?, #validate_config

Constructor Details

#initialize(config: nil, executor: nil, logger: nil) ⇒ Base

Initialize the provider

Parameters:

  • config (ProviderConfig, nil) (defaults to: nil)

    provider configuration

  • executor (CommandExecutor, nil) (defaults to: nil)

    command executor

  • logger (Logger, nil) (defaults to: nil)

    logger instance



42
43
44
45
46
# File 'lib/agent_harness/providers/base.rb', line 42

def initialize(config: nil, executor: nil, logger: nil)
  @config = config || ProviderConfig.new(self.class.provider_name)
  @executor = executor || AgentHarness.configuration.command_executor
  @logger = logger || AgentHarness.logger
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



35
36
37
# File 'lib/agent_harness/providers/base.rb', line 35

def config
  @config
end

#executorObject (readonly)

Returns the value of attribute executor.



35
36
37
# File 'lib/agent_harness/providers/base.rb', line 35

def executor
  @executor
end

#loggerObject (readonly)

Returns the value of attribute logger.



35
36
37
# File 'lib/agent_harness/providers/base.rb', line 35

def logger
  @logger
end

Instance Method Details

#configure(options = {}) ⇒ self

Configure the provider instance

Parameters:

  • options (Hash) (defaults to: {})

    configuration options

Returns:

  • (self)


52
53
54
55
# File 'lib/agent_harness/providers/base.rb', line 52

def configure(options = {})
  @config.merge!(options)
  self
end

#display_nameString

Human-friendly display name

Returns:

  • (String)

    display name



99
100
101
# File 'lib/agent_harness/providers/base.rb', line 99

def display_name
  name.capitalize
end

#nameString

Provider name for display

Returns:

  • (String)

    display name



92
93
94
# File 'lib/agent_harness/providers/base.rb', line 92

def name
  self.class.provider_name.to_s
end

#send_message(prompt:, **options) ⇒ Response

Main send_message implementation

Parameters:

  • prompt (String)

    the prompt to send

  • options (Hash)

    additional options

Returns:



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
# File 'lib/agent_harness/providers/base.rb', line 62

def send_message(prompt:, **options)
  log_debug("send_message_start", prompt_length: prompt.length, options: options.keys)

  # Build command
  command = build_command(prompt, options)

  # Calculate timeout
  timeout = options[:timeout] || @config.timeout || default_timeout

  # Execute command
  start_time = Time.now
  result = execute_with_timeout(command, timeout: timeout, env: build_env(options))
  duration = Time.now - start_time

  # Parse response
  response = parse_response(result, duration: duration)

  # Track tokens
  track_tokens(response) if response.tokens

  log_debug("send_message_complete", duration: duration, tokens: response.tokens)

  response
rescue => e
  handle_error(e, prompt: prompt, options: options)
end