Class: AgentHarness::ProviderRuntime

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_harness/provider_runtime.rb

Overview

Normalized runtime configuration for per-request provider overrides.

ProviderRuntime lets callers pass a single, provider-agnostic payload into send_message that each provider materializes into CLI args, env vars, or config files as needed.

Examples:

Routing OpenCode through OpenRouter with a specific model

runtime = AgentHarness::ProviderRuntime.new(
  model: "anthropic/claude-opus-4.1",
  base_url: "https://openrouter.ai/api/v1",
  api_provider: "openrouter",
  env: { "OPENROUTER_API_KEY" => "sk-..." }
)
provider.send_message(prompt: "Hello", provider_runtime: runtime)

Passing a Hash (auto-coerced by Base#send_message)

provider.send_message(
  prompt: "Hello",
  provider_runtime: {
    model: "openai/gpt-5.3-codex",
    base_url: "https://openrouter.ai/api/v1"
  }
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil, base_url: nil, api_provider: nil, env: {}, flags: [], metadata: {}) ⇒ ProviderRuntime

Returns a new instance of ProviderRuntime.

Parameters:

  • model (String, nil) (defaults to: nil)

    model identifier override

  • base_url (String, nil) (defaults to: nil)

    upstream API base URL override

  • api_provider (String, nil) (defaults to: nil)

    API-compatible backend name

  • env (Hash<String,String>) (defaults to: {})

    extra environment variables for the subprocess

  • flags (Array<String>) (defaults to: [])

    extra CLI flags to append

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

    arbitrary provider-specific data



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
71
72
73
74
# File 'lib/agent_harness/provider_runtime.rb', line 36

def initialize(model: nil, base_url: nil, api_provider: nil, env: {}, flags: [], metadata: {})
  @model = model
  @base_url = base_url
  @api_provider = api_provider

  env_hash = env || {}
  unless env_hash.is_a?(Hash)
    raise ArgumentError, "env must be a Hash (got #{env_hash.class})"
  end
  normalized_env = env_hash.each_with_object({}) do |(key, value), acc|
    string_key = key.to_s
    unless value.is_a?(String)
      raise ArgumentError, "env value for #{string_key.inspect} must be a String (got #{value.class})"
    end
    acc[string_key] = value
  end
  @env = normalized_env.freeze

  normalized_flags = flags || []
  unless normalized_flags.is_a?(Array)
    raise ArgumentError, "flags must be an Array (got #{normalized_flags.class})"
  end
  normalized_flags = normalized_flags.dup
  normalized_flags.each_with_index do |flag, index|
    unless flag.is_a?(String)
      raise ArgumentError,
        "flags must be an Array of Strings; invalid element at index #{index}: #{flag.inspect} (#{flag.class})"
    end
  end
  @flags = normalized_flags.freeze

   =  || {}
  unless .is_a?(Hash)
    raise ArgumentError, "metadata must be a Hash (got #{.class})"
  end
  @metadata = .dup.freeze

  freeze
end

Instance Attribute Details

#api_providerObject (readonly)

Returns the value of attribute api_provider.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def api_provider
  @api_provider
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def base_url
  @base_url
end

#envObject (readonly)

Returns the value of attribute env.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def env
  @env
end

#flagsObject (readonly)

Returns the value of attribute flags.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def flags
  @flags
end

#metadataObject (readonly)

Returns the value of attribute metadata.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def 
  @metadata
end

#modelObject (readonly)

Returns the value of attribute model.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def model
  @model
end

Class Method Details

.from_hash(hash) ⇒ ProviderRuntime

Build a ProviderRuntime from a Hash.

Parameters:

  • hash (Hash)

    runtime attributes

Returns:

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/agent_harness/provider_runtime.rb', line 80

def self.from_hash(hash)
  raise ArgumentError, "expected a Hash, got #{hash.class}" unless hash.is_a?(Hash)

  new(
    model: hash[:model] || hash["model"],
    base_url: hash[:base_url] || hash["base_url"],
    api_provider: hash[:api_provider] || hash["api_provider"],
    env: hash[:env] || hash["env"] || {},
    flags: hash[:flags] || hash["flags"] || [],
    metadata: hash[:metadata] || hash["metadata"] || {}
  )
end

.wrap(value) ⇒ ProviderRuntime?

Coerce a value into a ProviderRuntime.

Parameters:

Returns:



97
98
99
100
101
102
103
104
105
# File 'lib/agent_harness/provider_runtime.rb', line 97

def self.wrap(value)
  case value
  when ProviderRuntime then value
  when Hash then from_hash(value)
  when nil then nil
  else
    raise ArgumentError, "Cannot coerce #{value.class} into ProviderRuntime"
  end
end

Instance Method Details

#empty?Boolean

Whether any meaningful overrides are present.

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/agent_harness/provider_runtime.rb', line 110

def empty?
  model.nil? && base_url.nil? && api_provider.nil? &&
    env.empty? && flags.empty? && .empty?
end